1. 程式人生 > 其它 >1084:不可以同時使用大於和小於

1084:不可以同時使用大於和小於

方法一:

select product_id,product_name
from
(
select a.product_id,product_name,
case when sale_date<='2019-03-31' and sale_date>='2019-01-01' then 0 else 1 end d
#casewhensale_datebetween'2019-01-01'and'2019-03-31'then0else1endd
from Sales a
left join Product b
on a.product_id=b.product_id
) a 
group by product_id
having sum(d)=0;

注意:這裡不可以這麼寫

#casewhen'2019-01-01'<=sale_date<='2019-03-31'then0else1endd。這麼寫相當於沒有寫。

方法二:直接group by然後having篩選日期條件

select a.product_id,product_name
from Sales a
left join Product b
on a.product_id=b.product_id
group by a.product_id
having min(sale_date)>='2019-01-01' and max(sale_date)<=
'2019-03-31' ;

方法三:太秀了

selecta.product_id,product_name fromSalesa leftjoinProductb ona.product_id=b.product_id groupbya.product_id havingsum(sale_datebetween'2019-01-01'and'2019-03-31')=count(*);