2014-04-08 127 views
0

訂購表格時遇到問題。ORDER BY字符串(可以是數字或字符串)將空字符串放到最後MySQL

我有一列字符串。這些字符串可以是數字(整數)。

現在我想通過升序排序,留在上面的數字和字符串在底部,與去年空的:

12 
23 
28 
33 
Hello 
Hello again 
(empty string) 
(empty string) 

這是問題,如果我去:

ORDER BY column ASC 

發生這種情況:

(empty string) 
(empty string) 
12 
23 
28 
33 
Hello 
Hello again 

我試着用:

ORDER BY CAST(column as unsigned) ASC, column ASC/DESC 

但是卻讓數值在底部:

Hello again 
Hello 
(empty string) 
(empty string) 
(empty string) 
12 
23 
28 
33 

我怎麼可能解決呢?非常感謝你!

回答

1
order by 
case 
when column is null then 1 else 0 
end, 
column 

OR

ORDER BY ISNULL(column), column ASC; 

OR

ORDER BY if(column = '' or column is null,1,0),column 
+0

謝謝你,但空字符串不是空的,但只是空 「」。所以這個解決方案不起作用。 – Pillo

+0

謝謝,第三種解決方案工作正常!再次感謝! – Pillo

+0

將case更改爲'when column =''then 1 else 0'。那樣有用嗎? – David

相關問題