假如有两个表
表1:product (productID,productName)
表2:consume (productID,nums) //nums表示对应的产品的销量
问题:列出每个产品ID,产品名,对应的销量
方法一:
select productID,productName,sum(nums) as nums from
(select c.productID as productID,c.nums as nums,p.productName as productName from consume as c,product as p where c.productID=p.productID)
as t group by productID,productName;
方法二:
select p.productID,p.productName,sum(c.nums)
from consume as c
join
product as p
on p.productID=c.productID
group by p.productID,p.productName;
因为查询字段中除了聚合函数外,有两个字段productID,productName,因此分组的列也需要有两个字段productID,productName
时间: 2024-10-07 06:35:03