2016-10-06 30 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 :

请让我们知道,如果您有任何阙。