Different approach of using IN clause in MySql

前端 未结 2 852
旧巷少年郎
旧巷少年郎 2020-12-13 04:53

Today I have posted an answer with a query like this

SELECT * FROM table_name where column_name IN (val1,val2,...)

Some another user has po

2条回答
  •  猫巷女王i
    2020-12-13 05:48

    you raised a question that is connected with my answer here.

    In a simple explanation using this statements below,

    SELECT * FROM TableName WHERE column1 IN (1, 2, 3, 4)
    -- versus
    SELECT * FROM TableName WHERE 1 IN (column1, column2, column3, column4)
    

    The first statement involves only ONE COLUMN that is being compared to multiple values.

    SELECT  *
    FROM   TableName
    WHERE  column1 = 1 OR
           column1 = 2 OR
           column1 = 3 OR
           column1 = 4
    

    while the second statement is A VALUE that is compared to multiple columns.

    SELECT  *
    FROM   TableName
    WHERE  column1 = 1 OR
           column2 = 1 OR
           column3 = 1 OR
           column4 = 1
    

    which is a bit different from one another.


    UPDATE 1

    Here's the third form of IN clause:

    • Multi-Column IN clause – Unexpected MySQL Issue

提交回复
热议问题