leetcode-1327 SQL 列出指定时间段内所有的下单产品 Apare_xzc

leetcode-1327 SQL 列出指定时间段内所有的下单产品

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

思路

我们可以先把Products表和Orders表通过product_id内连接

Products join Orders using (product_id)
//or
Products join Orders on Products.product_id=Orders.product_id

然后我们从连接好的表中查询出2020年2月的所有元组

where order_date like '2020-02%' 
//or
where order_date between '2020-02-01' and '2020-02-29'

然后我们把查到的2020-02的所有元组按照product_id分组(按照product_name分组也可)

group by product_id

然后我们查询所有unit值之和大于100的分组

having sum(unit)>=100

最后提取出product_name和unit(sum)

 product_name,sum(unit) as unit

好了,我们可以写出完整的sql查询语句了

select product_name,sum(unit) as unit from 
Products join Orders using (product_id) 
where order_date like '2020-02%' 
group by product_id having sum(unit)>=100;

在这里插入图片描述


猜你喜欢

转载自blog.csdn.net/qq_40531479/article/details/108506254