检查多个列中的一个值 [英] Checking multiple columns for one value

查看:40
本文介绍了检查多个列中的一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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数据处理动手指南

这篇关于检查多个列中的一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆