mysql中替代charindex的函數substring_index、find_in_set


表結構如下:
mysql> select * from test;
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
| 5 | test5 |
+----+-------+

執行以下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)

這個select在mysql中得結果會自動按照id升序排列,
但是我想執行"select * from test where id in(3,1,5);"的結果按照in中得條件排序,即:3,1,5,

想得到的結果如下:
id name
3 test3
1 test1
5 test5

請問在這樣的SQL在Mysql中怎么寫?
網上查到sqlserver中可以用order by charindex解決,但是沒看到Mysql怎么解決??請高手幫忙,謝

謝!

select * from a order by substring_index('3,1,2',id,1);

試下這個good,ls正解。


order by find_in_set(id,'3,1,5')

謝謝,經測試order by substring_index和order by find_in_set都可以


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM