2017-08-09 107 views
2

我有一張表,其中包含所有國家名稱以及金額字段的列表。我需要的是在上升的名字以顯示與量,在頂部爲了不等於0的國名,然後用電量的值顯示該國0.1以非空值先顯示結果然後用空值顯示結果

Name   Amount 
Afghanistan 0 
Argentina  0 
China   0.1 
Crotia   0 
Hongkong  0.08 
India   0.2 
Singapore  0.007 
Taiwan   0.22 

所需的輸出:

Name   Amount 
China   0.1 
Hongkong  0.08 
India   0.2 
Singapore  0.007 
Taiwan   0.22 
Afghanistan 0 
Argentina  0 
Crotia   0 

我已經嘗試過,直到現在,但它不工作。

select * from countries where amount !=0 ORDER by name ASC 
UNION ALL 
SELECT * from countries where amount == 0 ORDER BY name ASC 

回答

2
SELECT * 
    FROM countries 
    ORDER BY (Amount=0), Name 
+0

向上投票。結果是正確的。 – Learning

2

您可以通過自定義CASE表達訂購:

SELECT * 
FROM countries 
ORDER BY 
    CASE WHEN amount > 0 THEN 0 ELSE 1 END, -- places zeroes last 
    amount          -- then order by the amount 
+0

這將通過量訂單,但我根據由他想要的結果線程啓動給定輸出相信通過與量> 0名稱進行排序,並把那些在底部0量。 – Learning

0

我認爲MySQL的對待0作爲空。通常,null總是返回false,所以不是檢查值是否爲0,而是檢查它是否爲空。所以SELECT * FROM國家,量不名ASC UNION ALL SELECT * FROM國家,金額爲空

0

也許你正在尋找的東西像空的訂單?

SELECT * FROM countries 
ORDER BY 
CONCAT(CASE amount WHEN 0 THEN "Z" ELSE "A" END,`name`) asc;