Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new to SQL and can't figure why this query returns no results BUT I do know it's becuase I'm using the OR condition incorrectly. If

Can someone please show the correct way to go about this?

Thanks in advance,
-DA

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;
Posted
Comments
[no name] 21-Jun-13 10:30am    
Try "(exch.name = 'AMEX'
OR exch.name = 'CBOE'
OR exch.name = 'ISE'
OR exch.name = 'PHLX') and see what happens
d.allen101 21-Jun-13 10:53am    
thank you, that was it!

This way:
SQL
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
 
Share this answer
 
Comments
d.allen101 21-Jun-13 10:52am    
Thank you! that solved it!!!!
Prasad_Kulkarni 24-Jun-13 2:42am    
Glad it helps!
I know, that your problem was solved, but the SQL IN operator[^] would better suit for your situation. Also the code makes more sense when you are reading it after time.
Check the example code segment:
SQL
AND exch.name IN ('AMEX', 'CBOE', 'ISE', 'PHLX')

The opposit is NOT IN.
 
Share this answer
 
Comments
d.allen101 21-Jun-13 12:02pm    
yeah that's actually what I did. it's more elegant...thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900