检查多个列中的一个值

问题描述:

例如,我有一个表,其中包含这样的列:

I have a table that has columns like this for example:

id,col1,col2,col3,col4

现在,我要检查col1, col2, col3, col4ANY是否具有传入的值.

Now, I want to check if ANY of col1, col2, col3, col4 have the passed in value.

要做的很长的路要走.

SELECT * FROM table WHERE (col1 = 123 OR col2 = 123 OR col3 = 123 OR col4 = 123);

我想这是IN的相反版本.

是否有更简单的方法来做我想做的事?

Is there an easier way to do what I want?

您可以使用IN谓词,如下所示:

You can use the IN predicate, like so:

SELECT * FROM table WHERE 123 IN(col1, col2, col3, col4);

SQL小提琴演示


SQL Fiddle Demo


它是IN的相反版本.

it's the opposite version of IN.

不,不是,这与您在问题中使用OR的方式相同.

No it is not, It is the same as using the ORs the way you did in your question.

谓词IN或集合成员资格定义为 1 :

The predicate IN or set membership is defined as1:

Value Expression可以是 2 的地方:

因此,使用值表达式123(这是一个文字)可以做到这一点.

So it is fine to do it this way, using the value expression 123, which is a literal.

1、2:来自以下位置的图像:仅用于普通人的SQL查询:SQL数据处理动手指南