All for Joomla All for Webmasters

Let's Play With Coding…

Querying Data with MySQL IN Operator

I personally like this operator when we need to query data where we need to check with many values for references. Following illustrate syntax of IN operator

/**
 * CodeZone.in
 * Author: Dineshkumar 
 * Contact: hi@codezone.in
 */
 SELECT 
    column1,column2,...
FROM
    table_name
WHERE 
 (expr|column_1) IN ('value1','value2',...);

In Operator can be used with UPDATE, INSERT,DELETE,etc in where clause operations

Even you can use NOT with IN operator to find results of column which are NOT IN given set of values. Which can be illustrated by syntax given below

/**
 * CodeZone.in
 * Author: Dineshkumar 
 * Contact: hi@codezone.in
 */
 SELECT 
    column1,column2,...
FROM
    table_name
WHERE 
 (expr|column_1)  NOT IN ('value1','value2',...);

An Example to illustrate IN operator is given below

/**
 * CodeZone.in
 * Author: Dineshkumar 
 * Contact: hi@codezone.in
 */
 SELECT *
FROM
    users
WHERE 
 city IN ('coimbatore','chennai','mumbai','hydrabad','delhi');

In above example it will select all user details from database whose city are coimbatore, chennai, mumbai, hydrabad, delhi.

Leave a Comment

Leave a Reply