2013-02-27 60 views
3

我创建sql fiddle我没有得到正确的数据在mysql query.any可以帮助我吗?

enter image description here和显示的结果,但我没有得到第二排事件PQR .ANY一个可以帮我请。

我想那些行的开始日期和结束日期如果不为空,那么end_date必须等于或大于今天,如果当前use和maxuses不为null,那么当前使用少于max使用或所有数据为null接受事件名称和ID。

预先感谢

+0

+1使用sqlfiddle。 – 2013-02-27 18:42:26

+0

你想显示id = 2,但是这个id全为空! – 2013-02-27 19:00:44

+0

我想从表中获得信息,如果给出了开始日期和结束日期,那么结束日期必须小于或等于今天的日期。如果最大用途是给定值,那么当前使用必须小于max_uses,并且如果日期为空且最大用途为null然后得到那些行。 – 2013-02-27 19:01:21

回答

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 
); 
+0

其工作完全谢谢你。有没有办法使用连接? – 2013-02-27 19:10:37

+0

很高兴帮助。反问:你为什么想在这里使用连接? – 2013-02-27 19:12:35

+0

如果你不介意,我想知道使用连接可以做到吗? – 2013-02-27 19:15:20

0

两个WHERE条款有start_date is not null约束(加上一些其他),并成为该记录start_date(或每隔一个场)null,使得它不会出现在结果中。

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

相关问题