snipt

Ctrl+h for KB shortcuts

MySQL

Mysql select multiple id and order by them

SELECT * FROM mytable WHERE id IN (11, 2, 5) ORDER BY FIELD(id, 11, 2, 5);
https://snipt.net/embed/d04dbeb565822bd4b6165b07e9083bb6/
/raw/d04dbeb565822bd4b6165b07e9083bb6/
d04dbeb565822bd4b6165b07e9083bb6
mysql
MySQL
1
2019-07-09T10:24:43
True
False
False
Sep 05, 2012 at 07:19 AM
/api/public/snipt/46716/
mysql-select-multiple-id-and-order-by-them
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="k">SELECT</span> <span class="o">*</span> <span class="k">FROM</span> <span class="n">mytable</span> <span class="k">WHERE</span> <span class="n">id</span> <span class="k">IN</span> <span class="p">(</span><span class="mi">11</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="k">ORDER</span> <span class="k">BY</span> <span class="nf">FIELD</span><span class="p">(</span><span class="n">id</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">);</span> </span></pre></div> </td></tr></table>
MySql