Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different approach of using IN clause in MySql

Tags:

People also ask

What is the use of in in MySQL?

The MySQL IN Operator The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions.

How can we define the clause from in MySQL?

customer_id = order_details. customer_id ORDER BY order_id; This MySQL FROM clause example uses the FROM clause to list two tables - customers and order_details. And we are using the FROM clause to specify an INNER JOIN between the customers and order_details tables based on the customer_id column in both tables.

What is the use of between and in operator in MySQL give example?

In SQL, BETWEEN is a logical operator used to select a range of values from the table of the database. Using the between the query, we can also check whether a value is in the provided range or not. BETWEEN in MySQL is generally used with the SELECT statements, and with INSERT, DELETE, and UPDATE queries.


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 posted the answer a query like this

SELECT * FROM table_name where val1 IN (column_name)

As you can see here the position of the column_name and values are interchanged.

From Mysql Docs

expr IN (value,...)

Returns 1 if expr is equal to any of the values in the IN list, else returns 0. If all values are constants, they are evaluated according to the type of expr and sorted. The search for the item then is done using a binary search. This means IN is very quick if the IN value list consists entirely of constants.

mysql> SELECT 2 IN (0,3,5,7);
-> 0

mysql> SELECT 'wefwf' IN ('wee','wefwf','weg');
-> 1

As it clearly says that the above one(my query) is correct. but both the above queries produce the same output.

Also why not the other approach in listed in Mysql Documentation?

This question serves as a canonical information source regarding the use of IN. Its purpose is to have detailed, high quality answers detailing the proper use on IN in queries.