开发者

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...


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...)


WHERE ((type <> 'economy') AND (datetime > now)) OR ((type = 'economy') AND (datediff(datetime,NOW()) > 14)

'


SELECT type, date(datetime) AS date 
FROM tbl1 
WHERE ( type <> 'economy' AND datetime > NOW() ) 
    OR ( type = 'economy' AND datediff(datetime, NOW()) > 14 )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜