-1

我是 SQL 新手,不知道为什么这个查询没有返回任何结果,但我知道这是因为我错误地使用了 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;
4

3 回答 3

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')
于 2013-06-21T14:30:14.263 回答
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;
于 2013-06-21T14:30:17.490 回答
1

试试这个查询并验证它是否是因为 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 in ('AMEX' , 'CBOE', 'ISE','PHLX')
ORDER BY trddata.id 
LIMIT 100;
于 2013-06-21T14:36:56.527 回答