前述
题目描述
leetcode 题目:1327. 列出指定时间段内所有的下单产品
Code
写法一
先过滤,再连表
sql
-- 写法一:先过滤再连表
select B.product_name, A.summ as unit
from(
select product_id,
sum(unit) as summ
from Orders
where order_date between '2020-02-01' and '2020-02-29'
group by product_id
having summ >= 100
) A
left join Products B
on A.product_id = B.product_id;
写法二
先连表,再过滤
sql
-- 写法二:先连表再过滤
select product_name, sum(unit) as unit
from Products P
left join Orders O
using(product_id)
-- where order_date between '2020-02-01' and '2020-02-29'
-- where order_date >= '2020-02-01' and order_date <='2020-02-29'
-- where year(order_date) = 2020 and month(order_date) = 02
where order_date like '2020-02%'
group by O.product_id
having sum(unit) >= 100;
不同的过滤条件写法,都行。