2017-01-31 46 views
0

我有一個問題。我有這樣的SQL:在sql中停止或驗證

SELECT * FROM `plnning` 
WHERE (`begin` <= "2017-01-31 10:00:00" AND "2017-01-31 10:00:00" < `end`) 
OR (`begin` < "2017-02-01 10:00:00" AND "2017-02-01 10:00:00" <= `end`) 
OR ("2017-01-31 10:00:00" <= `begin` AND "2017-02-01 10:00:00" >= `end`) 
AND source = 2 

當我執行SQL我有這樣的數據:

id  begin     end     source 
1  2017-01-31 10:00:00 2017-02-01 10:00:00  1 

奇怪,因爲= 1,但在SQL我搜索源= 2,I假設源,從OR在條款。我如何解決這個問題?請幫幫我。 Thx提前對不起我的英文

回答

3

您應該將所有條件放在括號中。看我已經把所有的條件轉換支架

SELECT * FROM `plnning` 
    WHERE ((`begin` <= "2017-01-31 10:00:00" AND "2017-01-31 10:00:00" < `end`) 
    OR (`begin` < "2017-02-01 10:00:00" AND "2017-02-01 10:00:00" <= `end`) 
    OR ("2017-01-31 10:00:00" <= `begin` AND "2017-02-01 10:00:00" >= `end`)) 
    AND source = 2 
3

你應該嘗試這樣

SELECT * FROM `plnning` 
WHERE ((`begin` <= "2017-01-31 10:00:00" AND "2017-01-31 10:00:00" < `end`) 
OR (`begin` < "2017-02-01 10:00:00" AND "2017-02-01 10:00:00" <= `end`) 
OR ("2017-01-31 10:00:00" <= `begin` AND "2017-02-01 10:00:00" >= `end`)) 
AND source = 2