开发者

Multiple OR Clauses in MySQL

开发者 https://www.devze.com 2023-01-03 05:55 出处:网络
I\'m trying to grab content where id = 3 OR id = 9 OR id =开发者_高级运维 100... Keep in mind, I can have a few hundred of these ids.

I'm trying to grab content where id = 3 OR id = 9 OR id =开发者_高级运维 100... Keep in mind, I can have a few hundred of these ids.

What is the most efficient way to write my query?

$sql = "SELECT name FROM artists WHERE (id=3 OR id=9 OR .... id-100)"


  ....
WHERE id IN (3, 9, 100, ...)


You may want to use the IN() function:

... WHERE id IN (3, 9, 100);

The IN() function syntax:

expr IN (value,...)

Check whether a value is within a set of values ...

Returns 1 if expr is equal to any of the values in the IN list, else returns 0.


SELECT * FROM table WHERE id IN (1, 2, 3 etc)

And call only the columns you need rather than using *!


You can use SQL IN condition

SELECT * FROM table WHERE ID IN ('1', '2', '3', '4');


SELECT * FROM table WHERE id IN (1,2,5)


SELECT * FROM table WHERE id IN (1,2,5);


$sql = "SELECT name FROM artists WHERE id REGEXP regex-pattern"

0

精彩评论

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

关注公众号