Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using if statement in where clause

With if in a where statement I'm trying to get all rows where datetime is larger then NOW(), but if type = 'economy', i would like to constraint it to only show if datediff between datetime and NOW() is larger than 14 days.

Something like:

Select type,date(datetime) as date
from tbl1
where datetime > NOW()
IF(type = 'economy') && datediff(datetime,NOW()) > 14
order by datetime desc

I've tried with case when also, but can't figure it out...

like image 573
Fløtti Avatar asked Apr 19 '26 21:04

Fløtti


1 Answers

You can just separate the criteria into 2 different comparisons:

select type,date(datetime) as date 
from tbl1 
where (datetime > NOW() AND type <> 'economy') OR
      (datediff(datetime, NOW()) > 14 AND type = 'economy')
order by datetime desc

(Not checked for syntax errors, but the idea should be right...)

like image 77
Jaymz Avatar answered Apr 22 '26 14:04

Jaymz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!