开发者

more efficient way of using sql OR?

开发者 https://www.devze.com 2023-02-02 20:58 出处:网络
i am wondering if thei开发者_运维知识库r is a better way of doing something like this: SELECT *

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

精彩评论

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