我没有在mysql查询中获得正确的数据。任何人都可以帮助我吗?

时间:2013-02-27 18:37:30

标签: mysql sql select join

我创建了sql fiddle

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

我想要那些开始日期和结束日期为if的行如果不为null,则end_date今天必须等于或大于当前使用和最大值不为null然后当前使用少于max use或所有数据为null接受事件名称和id

提前致谢

3 个答案:

答案 0 :(得分: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之外,所有数据都是null。

我已经纠正的另一个错误是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
);

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我想这就是你要找的东西

   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