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