MySQL 中的多个 OR 子句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3030906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:19:18  来源:igfitidea点击:

Multiple OR Clauses in MySQL

sqlmysqldatabase

提问by Grant

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.

我正在尝试获取 id = 3 OR id = 9 OR id = 100 的内容...请记住,我可以拥有几百个这样的 ID。

What is the most efficient way to write my query?

编写查询的最有效方法是什么?

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

回答by Ignacio Vazquez-Abrams

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

回答by Daniel Vassallo

You may want to use the IN()function:

您可能想要使用该IN()功能:

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

The IN()function syntax:

IN()函数的语法:

expr IN (value,...)

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

Returns 1if expris equal to any of the values in the INlist, else returns 0.

expr IN (value,...)

检查一个值是否在一组值中......

返回1如果expr等于任何的值的IN列表,否则返回0

回答by Waleed Al-Balooshi

You can use SQL IN condition

您可以使用 SQL IN 条件

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

回答by Waleed Al-Balooshi

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

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

并且只调用您需要的列而不是使用*!

回答by Darin Dimitrov

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

回答by abatishchev

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

回答by HKVN

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

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