来自不同表的Sql sum()列

时间:2016-02-16 05:40:56

标签: php mysql sql database

类别表

mysql> SELECT * FROM cats;
+------+------+-----------+
| c_id | p_id | c_name    |
+------+------+-----------+
|    1 |    1 | cats 1    |
|    2 |    1 | cats 2    |
|    3 |    1 | cats 3    |
+------+------+-----------+

元表

mysql> SELECT * FROM meta;
+------+------+------+---------+-------------+-------+
| m_id | p_id | c_id | name    | description | costs |
+------+------+------+---------+-------------+-------+
|    1 |    1 |    1 | Abhijit | description | 100   |
|    2 |    1 |    1 | Abhijit | description | 200   |
|    3 |    1 |    2 | Abhiji2 | description | 500   |
+------+------+------+---------+-------------+-------+

交易表

mysql> SELECT * FROM transactions;
+------+------+------+---------------------+--------+
| t_id | p_id | m_id | date                | amount |
+------+------+------+---------------------+--------+
|    1 |    1 |    1 | 2016-02-16 11:17:06 | 50     |
|    2 |    1 |    1 | 2016-02-16 11:17:06 | 50     |
|    3 |    1 |    2 | 2016-02-16 11:17:06 | 50     |
|    4 |    1 |    2 | 2016-02-16 11:17:06 | 150    |
+------+------+------+---------------------+--------+

我想对每个类别成本(来自元表)和金额(来自交易表)求和(。)

我用:

mysql> SELECT c.*, SUM(t.amount), SUM(m.costs)
    FROM cats c
        LEFT JOIN meta m ON m.c_id=c.c_id
        LEFT JOIN transactions t ON t.m_id=m.m_id
    GROUP BY c.c_id;

+------+------+-----------+--------+---------------+--------------+
| c_id | p_id | c_name    | add_by | SUM(t.amount) | SUM(m.costs) |
+------+------+-----------+--------+---------------+--------------+
|    1 |    1 | Abhijit   |      1 |           100 |          400 |
|    2 |    1 | Abhiji2   |      1 |           200 |          500 |
+------+------+-----------+--------+---------------+--------------+

这是错的。猫身份 1 的费用 300 ,但此处我 400

我想从这样的查询中获得回报:

+------+------+-----------+--------+---------------+--------------+
| c_id | p_id | c_name    | add_by | SUM(t.amount) | SUM(m.costs) |
+------+------+-----------+--------+---------------+--------------+
|    1 |    1 | Abhijit   |      1 |           100 |          300 |
|    2 |    1 | Abhiji2   |      1 |           200 |          500 |
+------+------+-----------+--------+---------------+--------------+

2 个答案:

答案 0 :(得分:2)

我认为您的某个JOIN条件中存在拼写错误(或错误)。我认为你的原始查询是这样的:

SELECT c.*, SUM(t.amount), SUM(m.costs)
FROM cats c
    LEFT JOIN meta m ON m.c_id = c.c_id
    LEFT JOIN transactions t ON t.m_id = m.c_id
GROUP BY c.c_id;

请仔细注意ON t.m_id = m.c_id,这与您的预期输出一致。无论如何,我按如下方式重新设计了您的查询:

SELECT c.c_id, c.p_id, c.c_name, t2.transactionCosts, t1.metaCosts
FROM cats c
LEFT JOIN
(
    SELECT c_id, SUM(costs) AS metaCosts
    FROM meta
    GROUP BY c_id
) t1
    ON c.c_id = t1.c_id
LEFT JOIN
(
    SELECT m_id, SUM(amount) AS transactionCosts
    FROM transactions
    GROUP BY m_id
) t2
    ON c.c_id = t2.m_id
WHERE t2.transactionCosts IS NOT NULL OR t1.metaCosts IS NOT NULL;

第一个子查询计算每个c_id的元总计,第二个子查询计算每个m_id的事务总计。然后将这些结果与cats表连接在一起,以获得最终结果。

请按照以下链接查看正在运行的演示:

SQLFiddle

答案 1 :(得分:0)

问题是您选择c。*但仅按c_id分组,在这种情况下您有2个选项。窗函数或子查询。

通过over(partition by):

SELECT c.*, 
       SUM(t.amount)over(partition by c.c_id) as amount,
       SUM(m.costs)over(partition by c.c_id) as cost
FROM con_cats c
    LEFT JOIN meta m ON m.c_id=c.c_id
    LEFT JOIN transactions t ON t.m_id=m.m_id;

通过子查询:

select a.*,b.amount,b.costs from con_cats a 
inner join
(SELECT c.c_id, SUM(t.amount) as amount, SUM(m.costs) as costs
FROM con_cats c
    LEFT JOIN meta m ON m.c_id=c.c_id
    LEFT JOIN transactions t ON t.m_id=m.m_id
GROUP BY c.c_id) b
on a.c_id = b.c_id;