刚刚问了一个与这个非常相似的问题......
目前我正在我的声明中做一个非常基本的 OrderBy。
SELECT * FROM tablename WHERE visible=1 ORDER BY position ASC, id DESC
问题是“position”的空字符串条目被视为 0。因此,所有 position 为空字符串的条目都出现在 1、2、3、4 的条目之前。例如:
'', '', '', 1, 2, 3, 4
或:
0, 0, 0, 1, 2, 3, 4
有没有办法实现如下排序:
1, 2, 3, 4, '', '', ''.
或:
1, 2, 3, 4, 0, 0, 0.
我认为该解决方案可能具有某种替换功能,但我无法找到可以满足我要求的功能。
最佳答案
SELECT *
FROM tablename
WHERE visible=1
ORDER BY
case when position in('', '0') then 1 else 0 end,
position ASC,
id DESC
关于MySQL Order By a number,最后一个空字符串(0's),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2052216/