开发者

more efficient way of using sql OR?

i am wondering if thei开发者_运维知识库r is a better way of doing something like this:

SELECT * 
FROM tableA 
WHERE colour='red' OR colour='greed' OR colour='blue' OR colour='yellow' 

is their anything like this:

SELECT * 
FROM tableA 
WHERE colour='red''greed''yellow'

cheers in advance


Try the SQL IN operator

SELECT * FROM tableA WHERE colour in ('red','greed','yellow');


Note that the IN operator will not handle a NULL case the way some people might expect:

SELECT *
FROM tbl
WHERE color = 'blue' OR color IS NULL

is not the same as:

SELECT *
FROM tbl
WHERE color IN ('blue', NULL)

While this may seem obvious to look for in code, it can be pernicious when the IN is a sub-select, and NULLs are in the inner results:

SELECT *
FROM tbl
WHERE color IN (SELECT color FROM tbl2)

And it exhibits possibly even more unexpected behavior in the NOT IN case


The IN Operator should be able to do what you need it to.

see http://www.w3schools.com/sql/sql_in.asp

Basically you could say

SELECT * FROM tableA WHERE colour IN ('red','green','yellow')

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜