0

需要按照我在函数顺序中给出的参数值的顺序获取 SQL 查询的结果集。

mysql> select * from state where state_name in ("UP","AP","Orissa","MP");

+------------+
| state_name |
+------------+
| AP         |
| MP         |
| UP         |
| Orissa     |
+------------+

4 行一组(0.00 秒)

请提出一个解决方案,该解决方案可以在不使用任何额外插入和创建的情况下提供帮助。

4

3 回答 3

2
select * from state where state_name in ("UP","AP","Orissa","MP")    
order by FIELD(state_name ,"UP","AP","Orissa","MP")
于 2014-12-06T13:30:04.663 回答
0

带ANSI sql CASE

select * from state where state_name in ("UP","AP","Orissa","MP")
order by case state_name 
         when "UP" then 1 
         when "AP" then 2 
         when "Orissa" then 3 
         when "MP" then 4 
         else 5 end;
于 2014-12-06T13:32:01.803 回答
0

老套:

SELECT
  State.*
FROM State
JOIN (SELECT 'UP' sn, '1' pos UNION ALL
      SELECT 'AP', '2' UNION ALL
      SELECT 'Orissa', '3' UNION ALL
      SELECT 'MP', '4') SortOrder
ON State.state_name = SortOrder.sn
ORDER BY SortOrder.pos;

SQL小提琴

于 2014-12-06T14:29:59.730 回答