2013-06-21 35 views
-1

我是SQL新手,無法確定此查詢返回的結果爲什麼沒有結果,但是我知道這是因爲我錯誤地使用了OR條件。如果如何在MySQL查詢中使用OR

是否有人可以顯示正確的方式去嗎?

SELECT trddata.id, trddata.ts, instr.name, instr.underlying, instr.expiration,instr.strike, instr.callput,opnint.vol, trdind.name ind,exch.name exch, trddata.price,trddata.bidprcbbo,trddata.askprcbbo 
FROM trdopt trddata 
JOIN instropt instr 
ON trddata.optid = instr.id 
JOIN trdindopt trdind 
ON trdind.id = trddata.ind 
JOIN exchopt exch 
ON trddata.exchcode = exch.id 
JOIN opnintopt opnint 
ON opnint.optid = trddata.optid 
WHERE opnint.ds = DATE_FORMAT(trddata.ts, '%Y-%m-%d') 
AND trddata.id >= 71125752 
AND trddata.ts <= '2013-06-20 16:30:36' 
AND instr.underlying = 'AAPL' 
AND exch.name = 'AMEX' 
OR exch.name = 'CBOE' 
OR exch.name = 'ISE' 
OR exch.name = 'PHLX' 
ORDER BY trddata.id 
LIMIT 100; 

回答

7

它看起來像你想一些括號:

AND (exch.name = 'AMEX' 
OR exch.name = 'CBOE' 
OR exch.name = 'ISE' 
OR exch.name = 'PHLX') 

您還可以使用IN:

AND exch.name IN ('AMEX','CBOE','ISE','PHLX') 
+0

謝謝你的工作! –

6

我認爲你應該使用() 例如

SELECT trddata.id, trddata.ts, instr.name, instr.underlying, instr.expiration,instr.strike, instr.callput,opnint.vol, trdind.name ind,exch.name exch, trddata.price,trddata.bidprcbbo,trddata.askprcbbo 
FROM trdopt trddata 
JOIN instropt instr 
ON trddata.optid = instr.id 
JOIN trdindopt trdind 
ON trdind.id = trddata.ind 
JOIN exchopt exch 
ON trddata.exchcode = exch.id 
JOIN opnintopt opnint 
ON opnint.optid = trddata.optid 
WHERE opnint.ds = DATE_FORMAT(trddata.ts, '%Y-%m-%d') 
AND trddata.id >= 71125752 
AND trddata.ts <= '2013-06-20 16:30:36' 
AND instr.underlying = 'AAPL' 
AND (exch.name = 'AMEX' 
OR exch.name = 'CBOE' 
OR exch.name = 'ISE' 
OR exch.name = 'PHLX') 
ORDER BY trddata.id 
LIMIT 100; 
+0

謝謝,就是這樣!再次感謝你! –

1

試試這個查詢和ve如果是由於OR條件導致的rify:

SELECT trddata.id, trddata.ts, instr.name, instr.underlying, instr.expiration,instr.strike, instr.callput,opnint.vol, trdind.name ind,exch.name exch, trddata.price,trddata.bidprcbbo,trddata.askprcbbo 
FROM trdopt trddata 
JOIN instropt instr 
ON trddata.optid = instr.id 
JOIN trdindopt trdind 
ON trdind.id = trddata.ind 
JOIN exchopt exch 
ON trddata.exchcode = exch.id 
JOIN opnintopt opnint 
ON opnint.optid = trddata.optid 
WHERE opnint.ds = DATE_FORMAT(trddata.ts, '%Y-%m-%d') 
AND trddata.id >= 71125752 
AND trddata.ts <= '2013-06-20 16:30:36' 
AND instr.underlying = 'AAPL' 
AND exch.name in ('AMEX' , 'CBOE', 'ISE','PHLX') 
ORDER BY trddata.id 
LIMIT 100; 
+0

謝謝!解決了它! –