【问题标题】:mysql query - How to write if condition on wheremysql 查询 - 如何在 where 上写 if 条件
【发布时间】:2018-12-24 07:21:43
【问题描述】:

我有查询检查是否 close_at >= '00:00' + 间隔 1 天,否则不添加间隔。我的查询是:

select * from table where now() between open_at and if(close_at >= '00:00' close_at + INTERVAL 1 day, close_at - INTERVAL 1 day)

【问题讨论】:

  • 您的问题到底是什么?请提供minimal reproducible example,包括预期和实际结果。
  • 解释这部分 '23:00' 在 open_at 和 if(close_at >= '00:00' close_at + INTERVAL 1 day, close_at - INTERVAL 1 day)'之间的位置'
  • 您不能在 WHERE 中执行计算。您需要选择该值并使用 HAVING
  • 分享您的样本数据预期输出
  • 例如,我有数据:没有 | open_at | close_at 1 | 08:00 | 23:00 2 | 08:00 | 00:00 3 | 09:00 | 02:00 如果 close_at >= 00:00 我必须添加间隔 1 天,我现在如何编写选择时间查询,否则不要添加间隔 1 天 @ZaynulAbadinTuhin

标签: jquery mysql database


【解决方案1】:

在 where 条件下,你可以根据自己的业务逻辑编写 case when 条件。

【讨论】:

    【解决方案2】:

    选择 id , naam 来自klanten WHERE IF('close_at' >= '00:00' 'close_at' + INTERVAL 1 day, 'close_at' - INTERVAL 1 day) LIKE 'some condition'

    结构是这样的,但我不知道什么是常量或属性,正确使用简单的引号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-22
      • 1970-01-01
      • 2021-12-02
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      相关资源
      最近更新 更多