如何将 sum() 的结果与 select 查询连接起来
How to join a sum()'s result with a select query
我有一个基本的 transactions
table,总共有 4 列,如下所示:
`transaction_id` int(11) NOT NULL AUTO_INCREMENT,
`fruit_id` int(11),
`person_id` int(11),
`quantity_bought` int(11),
示例select *
查询:
transaction_id
fruit_id
person_id
quantity_bought
1
banana
alex
65
2
banana
joe
25
3
banana
jenny
70
4
apple
dan
80
4
apple
danny
50
我需要将每个水果的 total_quantity_bought
添加到此 select 查询的每一行:
SELECT transactions.* FROM transactions;
这是所需输出的示例:
transaction_id
fruit_id
person_id
quantity_bought
total_quantity_bought
1
banana
alex
65
160
2
banana
joe
25
160
3
banana
jenny
70
160
4
apple
dan
80
130
4
apple
danny
50
130
这是我迄今为止尝试过的方法,但惨遭失败:
select x.* , y.total_quantity_bought
from
(SELECT *
FROM transactions) x
cross join
(SELECT fruit_id , SUM(quantity_bought) AS total_quantity_bought
FROM transactions
GROUP BY fruit_id) y
我想为这样的总和创建一个 View
,但我正在寻找一个不需要 View
的解决方案
fruit_id
total_quantity_bought
banana
160
apple
130
感谢任何帮助。
你基本上就在那里。您的 JOIN
想要指定左侧的所有内容(来自 transactions
table)应该连接到右侧的一个关联总数(totals
子查询)...并且该连接应该以匹配 fruit_id
.
为条件
随着数据集的增长,此查询的性能可能会(严重)下降。您可能想要查看缓存总计(使用数据库触发器等)或在其他地方的代码中呈现总计。
如果您选择后者(在别处渲染),您可能会对 WITH ROLLUP
函数感兴趣:
https://mariadb.com/kb/en/select-with-rollup/
SELECT
transactions.*,
totals.total_quantity_bought
FROM
transactions -- These are the individual transactions
INNER JOIN -- Join each individual transaction against the calculated totals
(
SELECT
fruit_id,
SUM(quantity_bought) AS total_quantity_bought
FROM transactions
GROUP BY fruit_id
) as totals -- These are the calculated totals (subqueried)
ON totals.fruit_id = transactions.fruit_id
-- This joins the individual records to the totals by `fruit_id`
可能我只是被打败了答案,但这是我的尝试
SELECT t.transaction_id, t.fruit_id, t.person_id, t.quantity_bought, grouped_fruit.total_quantity_bought
FROM transactions t
JOIN (
SELECT fruit_id, SUM(quantity_bought) total_quantity_bought
FROM transactions
GROUP BY fruit_id
) grouped_fruit ON t.fruit_id = grouped_fruit.fruit_id
我有一个基本的 transactions
table,总共有 4 列,如下所示:
`transaction_id` int(11) NOT NULL AUTO_INCREMENT,
`fruit_id` int(11),
`person_id` int(11),
`quantity_bought` int(11),
示例select *
查询:
transaction_id | fruit_id | person_id | quantity_bought |
---|---|---|---|
1 | banana | alex | 65 |
2 | banana | joe | 25 |
3 | banana | jenny | 70 |
4 | apple | dan | 80 |
4 | apple | danny | 50 |
我需要将每个水果的 total_quantity_bought
添加到此 select 查询的每一行:
SELECT transactions.* FROM transactions;
这是所需输出的示例:
transaction_id | fruit_id | person_id | quantity_bought | total_quantity_bought |
---|---|---|---|---|
1 | banana | alex | 65 | 160 |
2 | banana | joe | 25 | 160 |
3 | banana | jenny | 70 | 160 |
4 | apple | dan | 80 | 130 |
4 | apple | danny | 50 | 130 |
这是我迄今为止尝试过的方法,但惨遭失败:
select x.* , y.total_quantity_bought
from
(SELECT *
FROM transactions) x
cross join
(SELECT fruit_id , SUM(quantity_bought) AS total_quantity_bought
FROM transactions
GROUP BY fruit_id) y
我想为这样的总和创建一个 View
,但我正在寻找一个不需要 View
fruit_id | total_quantity_bought |
---|---|
banana | 160 |
apple | 130 |
感谢任何帮助。
你基本上就在那里。您的 JOIN
想要指定左侧的所有内容(来自 transactions
table)应该连接到右侧的一个关联总数(totals
子查询)...并且该连接应该以匹配 fruit_id
.
随着数据集的增长,此查询的性能可能会(严重)下降。您可能想要查看缓存总计(使用数据库触发器等)或在其他地方的代码中呈现总计。
如果您选择后者(在别处渲染),您可能会对 WITH ROLLUP
函数感兴趣:
https://mariadb.com/kb/en/select-with-rollup/
SELECT
transactions.*,
totals.total_quantity_bought
FROM
transactions -- These are the individual transactions
INNER JOIN -- Join each individual transaction against the calculated totals
(
SELECT
fruit_id,
SUM(quantity_bought) AS total_quantity_bought
FROM transactions
GROUP BY fruit_id
) as totals -- These are the calculated totals (subqueried)
ON totals.fruit_id = transactions.fruit_id
-- This joins the individual records to the totals by `fruit_id`
可能我只是被打败了答案,但这是我的尝试
SELECT t.transaction_id, t.fruit_id, t.person_id, t.quantity_bought, grouped_fruit.total_quantity_bought
FROM transactions t
JOIN (
SELECT fruit_id, SUM(quantity_bought) total_quantity_bought
FROM transactions
GROUP BY fruit_id
) grouped_fruit ON t.fruit_id = grouped_fruit.fruit_id