2016-09-19 59 views
0

我想在SQL語法其他查詢查詢之間的結合之間的結合,我想與任何其他的SQL語法

SELECT * 
FROM tx_log 
WHERE (tx_command = 'prepaid-response' AND tx_status = 'OK') 
OR (tx_command = 'postpaid-response' AND tx_status = 'OK') 
OR (tx_command = 'cek-response' AND tx_status = 'NOK') 
    AND tx_timestamp between '2016-09-19 00:00:01' and '2016-09-20 00:00:01'; 

語法來說明以上數據很明顯,沒有顯示錯誤信息,但顯示的數據不不符合指定的日期。在上面我會顯示從2016-09-19直到2016-09-20的數據,但實際顯示的日期除外。

謝謝。

回答

1

試試這個

SELECT * 
FROM tx_log 
WHERE ((tx_command = 'prepaid-response' AND tx_status = 'OK') 
OR (tx_command = 'postpaid-response' AND tx_status = 'OK') 
OR (tx_command = 'cek-response' AND tx_status = 'NOK')) 
AND tx_timestamp between '2016-09-19 00:00:01' and '2016-09-20 00:00:01'; 

你錯過外部支架()。當所有OR條件結束時。

+0

哦,上帝呀,這是工作,謝謝。和簡單的解釋:) –

+0

@Rofi Fathurrohman我很樂意幫助你。上帝祝福你。 – Manish

1
SELECT * 
FROM tx_log 
WHERE ((tx_command = 'prepaid-response' AND tx_status = 'OK') 
OR (tx_command = 'postpaid-response' AND tx_status = 'OK') 
OR (tx_command = 'cek-response' AND tx_status = 'NOK')) 
    AND tx_timestamp between '2016-09-19 00:00:01' and '2016-09-20 00:00:01'; 

如果任何由OR返回true加盟條件則該行會被納入結果不管其他條件是否返回truefalse。將所有由OR加入的條件合併爲上述查詢中給出的單個條件。

+0

是的,它的工作,謝謝明確的解釋:) –

+0

@RofiFathurrohman不客氣。樂於幫助 –

1

「和」 優先於 「OR」 讓你查詢讀:

SELECT * 
FROM tx_log 
WHERE (tx_command = 'prepaid-response' AND tx_status = 'OK') 
OR (tx_command = 'postpaid-response' AND tx_status = 'OK') 
OR ((tx_command = 'cek-response' AND tx_status = 'NOK') 
     AND tx_timestamp between '2016-09-19 00:00:01' and '2016-09-20 00:00:01' 
    ) 

所以時間戳過濾僅適用於 「最後」 或條件

試試這個:

SELECT * 
FROM tx_log 
WHERE (
     (tx_command = 'prepaid-response' AND tx_status = 'OK') 
    OR (tx_command = 'postpaid-response' AND tx_status = 'OK') 
    OR (tx_command = 'cek-response' AND tx_status = 'NOK') 
    ) 
    AND tx_timestamp between '2016-09-19 00:00:01' and '2016-09-20 00:00:01'