【问题标题】:i am not getting proper data in mysql query.any one can help me?我在 mysql 查询中没有得到正确的数据。任何人都可以帮助我吗?
【发布时间】:2013-02-27 18:37:29
【问题描述】:

我创建了sql fiddle

并显示结果,但我没有收到第二行事件 pqr。任何人都可以帮助我。

我想要那些开始日期和结束日期的行,如果不为空,那么 end_date 今天必须等于或大于,如果当前使用和最大使用不为空,那么当前使用小于最大使用或所有数据为空,接受事件名称和 id .

提前致谢

【问题讨论】:

  • +1 用于使用 sqlfiddle。
  • 你想显示 id = 2 ,但是这个 id 都是空的!!
  • 如果给出了开始日期和结束日期,我想从表中获取信息,那么结束日期必须小于或等于今天的日期。如果给定了最大使用量,则当前使用量必须小于 max_uses 并且如果日期为空,最大使用次数为空,然后获取该行。
  • 请问你想得到什么结果?只有 id = 6 ?

标签: mysql sql select join


【解决方案1】:

您的 Select 语句如下所示:

SELECT * FROM event 
WHERE (
     (start_date IS NOT NULL)
  && (end_date IS NOT NULL)
  && (end_date >= curdate())
)
|| (
     (max_use IS NOT NULL)
  && (current_use IS NOT NULL)
  && (current_use < max_use)
);

这并不是你真正想要的。特别是你完全错过了or all data is null except event name and id 部分。还有其他错误(在您的问题文本或陈述中)。

pqr 行就是这样的一行,除了事件名称和 ID 之外,所有数据都为空。

我已经纠正的另一个错误是 greater or equal 部分,您只检查了 greater

你很可能想要这个statement

SELECT * FROM event 
WHERE (
     start_date IS NOT NULL
  && end_date IS NOT NULL
  && end_date >= curdate()
)
||(
     max_use IS NOT NULL
  && current_use IS NOT NULL
  && current_use < max_use
)
|| (
     event is not null
  && start_date is null
  && end_date is null
  && max_use is null
  && current_use is null
);

【讨论】:

  • 它工作得很好,谢谢。有什么办法可以使用连接吗?
  • 很高兴为您提供帮助。反问:为什么要在这里使用连接?
  • 如果你不介意。我想知道它可以使用连接来完成吗?
【解决方案2】:

两个WHERE 子句都有一个start_date is not null 约束(加上其他一些约束),并且该记录是start_date(或所有其他字段)null,使其不会出现在结果中。

【讨论】:

    【解决方案3】:

    我猜这就是你要找的东西

       select * from event 
      where ((start_date is not null)&&(end_date is not null)&&(end_date>= curdate()))
    
     OR ((max_use is not null)&&(current_use is not null)&&(current_use < max_use ))
     or ( (start_date is  null ) and (end_date is  null) and (max_use is  null)
       and (current_use is  null))
    

    DEMO SQLFIDDLE

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多