时间:2021-07-01 10:21:17 帮助过:36人阅读
假如有两个表
表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
例题:数据库查询结果作为一个表
标签:表示 查询 name 数据 from rod 数据库 group join