如何在SQL查询中使用OR [英] How to use OR in SQL query

查看:281
本文介绍了如何在SQL查询中使用OR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是SQL的新手,无法理解为什么这个查询没有返回结果但是我知道这是因为我正在使用OR条件不正确。如果



有人可以说出正确的方法吗?



提前致谢,

-DA



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;

推荐答案

这样:

This way:
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


我知道,你的问题已经解决,但 SQL IN运营商 [ ^ ]更适合您的情况。当你在一段时间后阅读它时代码也更有意义。

检查示例代码段:

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:
AND exch.name IN ('AMEX', 'CBOE', 'ISE', 'PHLX')



对立面不是。


The opposit is NOT IN.


这篇关于如何在SQL查询中使用OR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆