《MYSQL教程Mysql select in 按id排序?qū)崿F(xiàn)方法》要點(diǎn):
本文介紹了MYSQL教程Mysql select in 按id排序?qū)崿F(xiàn)方法,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
表結(jié)構(gòu)如下:
mysql> select * from test;
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
| 5 | test5 |
+----+-------+
執(zhí)行以下SQL:
mysql> select * from test where id in(3,1,5);
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 3 | test3 |
| 5 | test5 |
+----+-------+
3 rows in set (0.00 sec)
這個(gè)select在mysql中得結(jié)果會(huì)自動(dòng)按照id升序排列,
但是我想執(zhí)行"select * from test where id in(3,1,5);"的結(jié)果按照in中得條件排序,即:3,1,5,
想得到的結(jié)果如下:
id name
3 test3
1 test1
5 test5
請(qǐng)問(wèn)在這樣的SQL在Mysql中怎么寫(xiě)?
網(wǎng)上查到sqlserver中可以用order by charindex解決,但是沒(méi)看到Mysql怎么解決??請(qǐng)高手幫忙,謝
謝!
select * from a order by substring_index('3,1,2',id,1);
試下這個(gè)good,ls正解.
order by find_in_set(id,'3,1,5')
謝謝,經(jīng)測(cè)試order by substring_index和order by find_in_set都可以
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/3852.html