0
有沒有辦法在MySQL中通過Word進行排序?選擇 - 順序在MySQL中的單詞?
例如:
SELECT * FROM `table` WHERE `batch` = 'something' AND outcome in ('Business','Professional', 'Consumer')
我想爲了行的列表:商務,專業,然後消費,而不是字母順序。
有沒有辦法在MySQL中通過Word進行排序?選擇 - 順序在MySQL中的單詞?
例如:
SELECT * FROM `table` WHERE `batch` = 'something' AND outcome in ('Business','Professional', 'Consumer')
我想爲了行的列表:商務,專業,然後消費,而不是字母順序。
試試這個:
SELECT *
FROM `table`
WHERE `batch` = 'something'
AND outcome in ('Business','Professional', 'Consumer')
ORDER BY
CASE WHEN outcome = 'Business' THEN 1
WHEN outcome = 'Professional' THEN 2
WHEN outcome = 'Consumer' THEN 3
ELSE 4
END
你可以結合每一個具體..試試這個。
SELECT * FROM `table` WHERE `batch` = 'something' AND outcome = 'Business'
UNION
SELECT * FROM `table` WHERE `batch` = 'something' AND outcome = 'Professional'
UNION
SELECT * FROM `table` WHERE `batch` = 'something' AND outcome = 'Consumer'
很不錯的!從來不知道'CASE WHEN'會在'ORDER BY'之後工作 – user1246800
很高興我能提供幫助。 – GGio