我有以下查询:
select `state` from `table1`
where `state` in ('NC','North Carolina','TN','Tennessee','CO','Colorado','NM','New Mexico','UT','Utah')
limit 200 offset 0
我需要state
排序的结果,因为它们出现在我的where
子句中(即第一个NC
,然后是TN
,然后是CO
,所以上)。
我该怎么做?我尝试使用order by
,但它没有给出这个结果。
答案 0 :(得分:6)
您可以使用 ORDER BY FIELD
SELECT `state`
FROM `table1`
WHERE `state` IN ('NC','North Carolina','TN','Tennessee','CO','Colorado','NM','New Mexico','UT','Utah')
ORDER BY FIELD(`state`, 'NC','North Carolina','TN','Tennessee','CO','Colorado','NM','New Mexico','UT','Utah');
答案 1 :(得分:3)
SELECT col_name
FROM table_nane
WHERE col_nane IN ('NC','North Carolina','TN','Tennessee','CO','Colorado','NM','New Mexico','UT','Utah')
ORDER BY FIELD(`state`, 'NC',.....);
依旧......
答案 2 :(得分:1)
试试这个:
order by case when `state`='NC' or `state`='North Carolina' then 1
when `state`='TN' or `state`='Tennessee' then 2
when `state`='CO' or `state`='Colorado' then 3
end
答案 3 :(得分:1)
您可以将ORDER BY
与CASE WHEN
结构合并:
ORDER BY CASE
WHEN state = 'NC' THEN 1
WHEN state = 'North Carolina' THEN 2
...
END