结合

时间:2017-12-08 00:50:47

标签: php mysql sql

首先,感谢您阅读我的问题。我非常感谢社区中的人们帮助他人更多地了解编码。由于这个SQL和PHP是我的业余爱好的一部分,我受到Google和论坛的限制,在那里我可以找到我的问题的答案。不幸的是,我找不到遇到问题的好答案。

我想做什么? 我建立了一个网站,您可以在其中记下您的公里数和升数,这样您就可以查看您的车辆并查看统计信我想在MySQL中用查询进行大搜索。我知道如何在多个查询中获得结果(确切地说是3个),但我需要它在一个查询中。

我的桌子看起来像什么? 举个例子,我使用这个表来使问题更具视觉效果,现在名为“ fillings ”。

+---------+---------+------------+--------+--------+------------+
| tank_id | user_id | kilometers | liters | car_id | date       |
+---------+---------+------------+--------+--------+------------+
| 1       | 2       | 450        | 20     | 2      | 2017-11-01 |
+---------+---------+------------+--------+--------+------------+
| 2       | 1       | 500        | 30     | 1      | 2017-12-15 |
+---------+---------+------------+--------+--------+------------+
| 3       | 2       | 490        | 25     | 2      | 2017-11-05 |
+---------+---------+------------+--------+--------+------------+
| 4       | 3       | 260        | 19     | 3      | 2017-11-07 |
+---------+---------+------------+--------+--------+------------+
| 5       | 3       | 610        | 30     | 3      | 2017-12-03 |
+---------+---------+------------+--------+--------+------------+
| 6       | 3       | 100        | 4      | 3      | 2017-12-07 |
+---------+---------+------------+--------+--------+------------+

这是我的主表的基本概念,其中所有填充都被注册。实际上,有人可以拥有多辆汽车,但现在让我们假设用户只有一辆汽车。

什么是SQL查询?
我想要的查询应该有这样的结果:

+---------+----------------------+---------------------+
| user_id | consumption november | consumption overall |
+---------+----------------------+---------------------+
| 2       | 20,9                 | 20,9                |
+---------+----------------------+---------------------+
| 1       |                      | 16,7                |
+---------+----------------------+---------------------+
| 3       | 13,7                 | 18,3                |
+---------+----------------------+---------------------+

如您所见,11月份的填充物正在计算,正在计算消耗量。此外,正在计算来自特定类型用户的所有填充物的总消耗量。我知道如何制作这个结果,分为两个查询。这是:

SELECT user_id, SUM(kilometers) / SUM(liters) as consumption overall
FROM fillings
GROUP BY user_id

还有查询:

SELECT user_id, SUM(kilometers) / SUM(liters) as consumption november
FROM fillings
WHERE 'date'
BETWEEN '2017-11-01' AND '2017-11-31'
GROUP BY user_id

所以现在我想将查询合并为一个,这样我就可以得到我想要的结果。请注意,在该示例中,某人可能在一个月内消耗掉,因为那个月他或她没有填满他们的汽车。

我已经研究了UNION和JOIN的选项,但我似乎无法找到一种方法来实现这一点。

我希望一切都清楚,如果没有,请不要犹豫,我会进一步详细解释。

提前致谢。

4 个答案:

答案 0 :(得分:2)

只需使用条件聚合:

SELECT user_id,
       (SUM(CASE WHEN date >= '2017-11-01' AND date < '2017-12-01'
                 THEN kilometers ELSE 0
            END) /
        SUM(CASE WHEN date >= '2017-11-01' AND date < '2017-12-01'
                 THEN liters
            END)
       ) as consumption_november,        
       SUM(kilometers) / SUM(liters) as consumption_overall
FROM fillings
GROUP BY user_id

答案 1 :(得分:2)

由于记录可能在一个月内不在范围内,因此建议对特定日期查询使用LEFT JOIN:

 SELECT 
 t1.user_id, t2.consumption_november, t1.consumption_overall 
 FROM (
   SELECT user_id, SUM(kilometers) / SUM(liters) as consumption_overall 
   FROM fillings 
   GROUP BY user_id
 ) t1
 LEFT JOIN 
 (
   SELECT user_id, SUM(kilometers) / SUM(liters) as consumption_november 
   FROM fillings 
   WHERE `date` BETWEEN '2017-11-01' AND '2017-11-31'
   GROUP BY user_id
 ) t2
 ON t1.user_id = t2.user_id;

http://sqlfiddle.com/#!9/3fdce84/16

答案 2 :(得分:1)

您可以尝试类似

的内容
SELECT t1.user_id, 
    t1.consumption_overall as consumption overall,
    t2.consumption_november as consumption november
FROM (
    SELECT user_id, SUM(kilometers) / SUM(liters) as consumption_overall
    FROM fillings
    GROUP BY user_id
) as t1, (
    SELECT user_id, SUM(kilometers) / SUM(liters) as consumption_november
    FROM fillings
    WHERE 'date'
    BETWEEN '2017-11-01' AND '2017-11-31'
    GROUP BY user_id
) as t2
WHERE t1.user_id = t2.user_id

它会将您的两个查询的结果放在同一个表中。

答案 3 :(得分:1)

对于扩展答案(在我的第一个答案中提到你的问题,因为作为评论留下的时间太长),请使用:

SELECT t1.user_id, t2.consumption_november, t3.consumption_q, t1.consumption_overall FROM ( SELECT user_id, SUM(kilometers) / SUM(liters) as consumption_overall FROM fillings GROUP BY user_id ) t1 LEFT JOIN ( SELECT user_id, SUM(kilometers) / SUM(liters) as consumption_november FROM fillings WHERE [date] BETWEEN '2017-11-01' AND '2017-11-31' GROUP BY user_id ) t2 ON t1.user_id = t2.user_id LEFT JOIN ( SELECT user_id, SUM(kilometers) / SUM(liters) as consumption_q FROM fillings WHERE [date] BETWEEN '2017-08-01' AND '2017-11-31' GROUP BY user_id ) t3 ON t1.user_id = t3.user_id;

对于后续的左连接,只需从最后的左连接中删除分号并扩展模式,然后在SELECT子句中添加新字段。