Checking multiple columns for one value

后端 未结 3 1094
名媛妹妹
名媛妹妹 2020-11-28 05:45

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

id,col1,col2,col3,col4

Now, I want to check if ANY of col1, col2,

3条回答
  •  青春惊慌失措
    2020-11-28 05:52

    You can use the IN predicate, like so:

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

    SQL Fiddle Demo


    it's the opposite version of IN.

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


    To clarify this:

    The predicate IN or set membership is defined as1:

    enter image description here

    Where the Value Expression can be either 2:

    enter image description here

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


    1, 2: Images from: SQL Queries for Mere Mortals(R): A Hands-On Guide to Data Manipulation in SQL

提交回复
热议问题