MYSQL查询-使用另一个表中的多个ID求和项
如图所示,我有两张桌子。我可以从item_transactions表中计算每个item_id的销售数量之和。在一个单独的列中,我想从I_multiple_int_attributes表中求出该商品所有以前版本的销售数量之和(有些商品最多有4个以前的版本)。有人能给我指出正确的方向吗 当前查询:MYSQL查询-使用另一个表中的多个ID求和项,mysql,sql,aggregate-functions,Mysql,Sql,Aggregate Functions,如图所示,我有两张桌子。我可以从item_transactions表中计算每个item_id的销售数量之和。在一个单独的列中,我想从I_multiple_int_attributes表中求出该商品所有以前版本的销售数量之和(有些商品最多有4个以前的版本)。有人能给我指出正确的方向吗 当前查询: SELECT t.item_id, SUM(t.qty_sold) as current_id_sales, SUM(?) as previous_id_sales FROM gnpcb.item_tra
SELECT t.item_id, SUM(t.qty_sold) as current_id_sales, SUM(?) as previous_id_sales
FROM gnpcb.item_transactions t join gnpcb.i_multiple_int_attributes a on t.item_id = a.id
and a.type = 'items' and a.attribute = 'previous_editions'
WHERE t.id_type in ('invoice', 'credit') and t.item_id IN ('18117', '17473', '17568')
and t.transaction_type in ('sale', 'return', 'return_nts')
GROUP BY t.item_id;
预期结果:
+---------+------------------+-------------------+
| item_id | current_id_sales | previous_id_sales |
+---------+------------------+-------------------+
| 17473 | 15743 | 9625 |
| 17568 | 3893 | 24232 |
| 18117 | 14430 | 8083 |
+---------+------------------+-------------------+
表1:项目交易
+---------+---------+----------+----------------+----------------+
| id_type | item_id | qty_sold | price_extended | date_effective |
+---------+---------+----------+----------------+----------------+
| invoice | 18117 | 8 | 13.1600 | 2016-10-01 |
| invoice | 17473 | 1 | 2.2500 | 2016-10-01 |
| invoice | 18117 | 1 | 1.0000 | 2016-10-01 |
| invoice | 18117 | 7 | 2.0000 | 2016-10-01 |
| invoice | 18117 | 5 | 3.0000 | 2016-10-01 |
| invoice | 17473 | 3 | 4.0000 | 2016-10-01 |
| invoice | 17568 | 1 | 4.0000 | 2016-10-01 |
| invoice | 17568 | 5 | 3.0000 | 2016-10-01 |
| invoice | 18117 | 8 | 2.0000 | 2016-10-01 |
| invoice | 17473 | 1 | 1.0000 | 2016-10-01 |
+---------+---------+----------+----------------+----------------+
表2:i_多个_int_属性
+-------+-------+------------+-------------------+-------+
| type | id | sort_order | attribute | value |
+-------+-------+------------+-------------------+-------+
| items | 17473 | 1 | previous_editions | 15743 |
| items | 17568 | 1 | previous_editions | 3893 |
| items | 17568 | 2 | previous_editions | 7626 |
| items | 18117 | 1 | previous_editions | 14430 |
| items | 18117 | 2 | previous_editions | 17337 |
| items | 18117 | 3 | previous_editions | 17123 |
| items | 18117 | 4 | previous_editions | 17614 |
+-------+-------+------------+-------------------+-------+
一种方法是“当前组”、“上一组”和“加入”
select curr.item_id, curr.sales, prev.sales
from (
SELECT t.item_id, SUM(t.qty_sold) as sales
FROM gnpcb.item_transactions t
join gnpcb.i_multiple_int_attributes a on t.item_id = a.id
and a.type = 'items' and a.attribute = 'previous_editions'
WHERE t.id_type in ('invoice', 'credit') and t.item_id IN ('18117', '17473', '17568') and t.transaction_type in ('sale', 'return', 'return_nts')
GROUP BY t.item_id) curr
left join(
SELECT a.id as item_id, SUM(t.qty_sold) as sales
FROM gnpcb.item_transactions t
join gnpcb.i_multiple_int_attributes a on t.item_id = a.value
and a.type = 'items' and a.attribute = 'previous_editions'
WHERE t.id_type in ('invoice', 'credit') and t.item_id IN ('18117', '17473', '17568') and t.transaction_type in ('sale', 'return', 'return_nts')
GROUP BY a.id) prev on curr.item_id = prev.item_id
一种方法是“当前组”、“上一组”和“加入”
select curr.item_id, curr.sales, prev.sales
from (
SELECT t.item_id, SUM(t.qty_sold) as sales
FROM gnpcb.item_transactions t
join gnpcb.i_multiple_int_attributes a on t.item_id = a.id
and a.type = 'items' and a.attribute = 'previous_editions'
WHERE t.id_type in ('invoice', 'credit') and t.item_id IN ('18117', '17473', '17568') and t.transaction_type in ('sale', 'return', 'return_nts')
GROUP BY t.item_id) curr
left join(
SELECT a.id as item_id, SUM(t.qty_sold) as sales
FROM gnpcb.item_transactions t
join gnpcb.i_multiple_int_attributes a on t.item_id = a.value
and a.type = 'items' and a.attribute = 'previous_editions'
WHERE t.id_type in ('invoice', 'credit') and t.item_id IN ('18117', '17473', '17568') and t.transaction_type in ('sale', 'return', 'return_nts')
GROUP BY a.id) prev on curr.item_id = prev.item_id
非常感谢。在您的第二个子查询WHERE子句中,我必须将“t.item\U id In”更改为“a.id In”),但这正是我所需要的。谢谢!在您的第二个子查询WHERE子句中,我必须将“t.item_id In”更改为“a.id In”),但这正是我所需要的。