我有 2 个表(客户、交易)。
customer
id name location
1 samuel AZ
2 mithun CA
3 Robert NY
etc..
transaction
id customer_id product_id
1 1 12000
2 1 12222
3 3 15000
etc.
有不同的产品和不同的位置
通过这些列,我需要找到购买某一特定产品的地点数量?
我尝试使用联接。
我的代码是
select c.id, c.location, t.product_id
from customer c join transaction t
on (c.id = t.cid);
我能够连接两个表,但我需要按 tid 计算事务数。
最佳答案
尝试对它们进行分组 - 这提供了购买特定产品的地点数量。这将包括位置的重复条目,但会回答您的交易数量位
select t.product_id, count(c.location)
from customer c join transaction t
on (c.id = t.cid)
group by 1;
关于mysql - 需要查找购买的每种产品的销售地点数量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46344730/