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
As noted in the question, the MySQL documentation gives the form of the IN clause as:
expr IN (value,...)
expr
is an expression that can only have a single value for a given row - this value can be a constant (such as 2), the name of a column (such as column_name
from the question) which will have a specific value on a given row, or any expression including functions, operators, constants and column_names that produces a single value for a given row.
The contents of the parentheses - value,...
- contains an expression that can be evaluated as supplying a list of values (potentially including an empty list of values, in which case the in
condition would be evaluated as false). This list of values could be:
val1,val2,...
) and/or one or more single-valued column names for a given row (such as column_name
) and/or a series of expressions that each produce a single value for a given row; orselect
clause that returns a set of values (such as select column_name from table where ...
) that can then be compared with the value of expr
(immediately preceding the IN
operator).To summarise: expr
must evaluate to a single value (for a given row), while the parenthesised set of values to be matched can be evaluated to 0, 1 or more values.
In this respect, MySQL operates the same way as any other variant of SQL that I have used.
Where MySQL does vary is that the entire expr IN (value,...)
clause itself evaluates to 0 or 1, which MySQL treats as false or true respectively - in most variants of SQL, the entire clause would evaluate to a boolean FALSE or TRUE.
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: