MySQL 选择 ..... 在哪里 .... 或

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17870/
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 11:56:08  来源:igfitidea点击:

Select ..... where .... OR

mysql

提问by jessilfp

Is there a way to select data where any one of multiple conditions occur on the same field?

有没有办法选择在同一字段上出现多个条件中的任何一个的数据?

Example: I would typically write a statement such as:

示例:我通常会写一个语句,例如:

select * from TABLE where field = 1 or field = 2 or field = 3

Is there a way to instead say something like:

有没有办法代替说:

select * from TABLE where field = 1 || 2 || 3

Any help is appreciated.

任何帮助表示赞赏。

回答by mercutio

Sure thing, the simplest way is this:

当然,最简单的方法是这样的:

select foo from bar where baz in (1,2,3)

回答by Michael Stum

select * from TABLE where field IN (1,2,3)

You can also conveniently combine this with a subquery that only returns one field:

您还可以方便地将其与仅返回一个字段的子查询结合使用:

    select * from TABLE where field IN (SELECT boom FROM anotherTable)

回答by Can Berk Güder

OR:

或者:

SELECT foo FROM bar WHERE baz BETWEEN 1 AND 3

回答by Mike Polen

select * from TABLE where field in (1, 2, 3)

select * from TABLE where field in (1, 2, 3)

回答by Lasse V. Karlsen

WHERE field IN (1, 2, 3)

回答by Re0sless

You can still use in for

您仍然可以使用 for

select *
from table
where field  = '1' or field = '2' or field = '3'

its just

只是

select * from table where field in ('1','2','3')

回答by S.Witch

while inis a shortcut for orand I wasn't sure how I could combine inwith and, I did it this way

whilein是一个快捷方式or,我不确定如何in与结合and,我是这样做的

 SELECT * FROM table
 WHERE column1='x' AND (column2='y' OR column2='z');