2016-10-06 147 views
1

這是我的SQL表結構:SQL查詢合併兩個查詢到一個空行

表1:細節

|--id--|--id_user--|--price--| 
    | 1 | 1  | 10 | 
    | 2 | 2  | 15 | 
    | 3 | 1  | 25 | 
    | 4 | 3  | 30 | 
    | 5 | 3  | 7  | 
    ------------------------------ 

表2:用戶

|--id--|--id_country--| 
    | 1 |  1  | 
    | 2 |  2  | 
    | 3 |  0  | 
    ----------------------- 

表3:國家

|--id--|--country--| 
    | 1 | France | 
    | 2 | Italy | 
    -------------------- 

我需要的是t Ø按國家得到價格的總和:

SELECT c.country, SUM(d.price) AS price 
    FROM details d 
     INNER JOIN users u ON u.id = d.id_user 
     INNER JOIN country c ON c.id = u.id_country 
    GROUP BY c.country 
    ORDER BY c.country 

我得到這個:

|--country--|--price--| 
| France | 35 | 
| Italy | 15 | 
----------------------- 

但我需要得到這個:

|--country--|--price--| 
| France | 35 | 
| Italy | 15 | 
| Undefined | 37 | 
----------------------- 

其中undefined是,如果id_country=0。 (我不能添加到國家表id=0 or id=undefined,它會弄亂其他東西)。現在,我通過兩個單獨的查詢實現這一目標,第二個是:

SELECT SUM(d.price) as price 
    FROM details d 
     INNER JOIN users u ON u.id = d.id_user AND u.id_country=0 
    GROUP BY u.id_country 

我在想,如果...... 是有可能做到這一點在一個查詢?

+1

切換到左加入國家。 – jarlh

+0

RIGHT JOIN'INNER JOIN country c' –

+0

很奇怪的是,您可以在不存在的用戶表中使用id_country。這表明你不正確地使用你的數據庫。如果你想允許「未定義的國家」,你應該有一個外鍵約束,並使列可以爲空。 –

回答

1

你需要使用左側加入在這種情況下:

SELECT c.country, SUM(d.price) AS price 
    FROM details d 
     LEFT JOIN users u ON u.id = d.id_user 
     LEFT JOIN country c ON c.id = u.id_country 
    GROUP BY c.country 
    ORDER BY c.country 

如果使用INNER JOIN,你只會得到存在兩個表中的結果。

要使用未定義使用替代NULL:

SELECT IFNULL(c.country,'Undefined') AS Country, SUM(d.price) AS price 
    FROM details d 
     LEFT JOIN users u ON u.id = d.id_user 
     LEFT JOIN country c ON c.id = u.id_country 
    GROUP BY c.country 
    ORDER BY c.country 

一種方式進行排序,以獲得未定義最後一個是添加的SortField

SELECT A.Country,A.Price FROM (
    SELECT IFNULL(c.country,'Undefined') AS Country, SUM(d.price) AS price, IFNULL(c.Country,'ZZZZZZZZ') AS Sort 
     FROM details d 
      LEFT JOIN users u ON u.id = d.id_user 
      LEFT JOIN country c ON c.id = u.id_country 
     GROUP BY c.country 
) A 
ORDER BY A.Sort 

編輯:ORDER BY的意見建議

SELECT IFNULL(c.country,'Undefined') AS Country, SUM(d.price) AS price 
    FROM details d 
     LEFT JOIN users u ON u.id = d.id_user 
     LEFT JOIN country c ON c.id = u.id_country 
    GROUP BY c.country 
    ORDER BY c.country IS NULL, c.country 
+0

您的權利是否可以將「null」更改爲「未定義」或其他單詞並插入結果表的開頭或結尾? – felixRo

+1

在最後使用時得到空值:'ORDER BY c.country IS NULL,c.country'。 –

+0

我喜歡這個以'SELECT IFNULL'開頭的中間答案,但是最後使用@ Thorsten-Kettner的建議'ORDER BY c.country IS NULL,c.country'並且效果很棒!謝謝你的答案。 – felixRo

0

請嘗試以下查詢。

 SELECT 
     CASE 
     WHEN c.country is NULL THEN 'Undefined' 
     ELSE c.country 
     END as country 
     , SUM(d.price) AS price 
     FROM users u 
     left JOIN details d ON u.id = d.id_user 
     left JOIN country c ON c.id = u.id_country 
     GROUP BY c.country 
     ORDER BY c.country 

對於演示:

SqlfiddlE Demo :

請讓我們知道,如果您有任何闕。