在 where 子句中使用 if 语句

发布于 2024-11-07 05:19:51 字数 364 浏览 2 评论 0原文

如果在 where 语句中,我试图获取日期时间大于 NOW() 的所有行,但如果 type = 'economy',我想将其限制为仅显示 datetime 和 NOW() 之间的 datediff 是否更大超过 14 天。

类似:

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

我也尝试过使用案例,但无法弄清楚......

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晚风撩人 2024-11-14 05:19:51

您可以将标准分为 2 个不同的比较:(

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

未检查语法错误,但想法应该是正确的......)

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

旧时浪漫 2024-11-14 05:19:51
WHERE ((type <> 'economy') AND (datetime > now)) OR ((type = 'economy') AND (datediff(datetime,NOW()) > 14)

'

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

'

久光 2024-11-14 05:19:51
SELECT type, date(datetime) AS date 
FROM tbl1 
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 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文