2013-02-27 55 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

相關問題