繁体   English   中英

sql查询将两个查询合并为一个空行

[英]sql query combine two queries into one with empty rows

这是我的 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    |
    --------------------

我需要的是按国家/地区获得价格总和:

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    |
-----------------------

如果id_country=0 ,则undefined会在哪里。 (我不能将id=0 or id=undefined添加到 country 表中,它会弄乱其他东西)。 现在我通过两个单独的查询来实现这一点,第二个是:

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

我在想是否...是否可以在一个查询中做到这一点?

在这种情况下,您需要使用左连接:

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 替换为 Undefined 使用:

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

排序以最后获得 Undefined 的一种方法是添加一个 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

试试下面的查询。

        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 演示:

如果您有任何疑问,请告诉我们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM