基本上我有一个字段可以包含一些前缀字符串:
word1
bla2
ttt3
word4
[...]
Run Code Online (Sandbox Code Playgroud)
我需要首先订购SQL,ttt3然后再订购所有其他字符串.
我试了这个没有运气
ORDER BY FIELD (myField,'ttt3',*)
Run Code Online (Sandbox Code Playgroud)
有什么建议?
使用ORDER BY CASE构造.它强制为您要提取的值为0,为其他所有值强制为1,因此0先排序.它只是ORDER BY像任何列一样放入列表中,以逗号分隔,因此您可以在之后添加其他排序列.
ORDER BY
CASE WHEN myField = 'ttt3' THEN 0 ELSE 1 END,
myField,
other_order_col,
other_order_col2
Run Code Online (Sandbox Code Playgroud)