我在sql查询中面临一些困难
select * from country order by
case when country_id in(254,43,253,91,82,175) then 0 else country.country_id end
ASC
我希望记录中的第一个案例条件是在查询中给出的顺序,如...
254
43
253
91
82
175
答案 0 :(得分:1)
如果您使用MySQL,我相信这是您想要的查询。
SELECT *
FROM country
ORDER BY FIELD(country_id, 175, 82, 91, 253, 43, 254) DESC -- reverse order here
, country_id ;
或者(对于任何SQL产品):
SELECT *
FROM country
ORDER BY CASE country_id
WHEN 254 THEN 1
WHEN 43 THEN 2
WHEN 253 THEN 3
WHEN 91 THEN 4
WHEN 82 THEN 5
WHEN 175 THEN 6
ELSE 7
END
, country_id ;
答案 1 :(得分:1)
如果您想要符合core SQL-99 standard
:
SELECT *
FROM country
ORDER BY
CASE
WHEN country_id IN (254,43,253,91,82,175) THEN 1
ELSE 666
END, country_id