Jump to: navigation, search

Mysql IN

From w3cyberlearnings

Contents

MySQL IN Function

Return 1 if expr is equal to any of the values in the list, and otherwise returns 0.

Syntax IN

  • Va1: value 1
  • Va2: value 2
  • Va3: value 3
expr IN(Va1,Va2,Va3...);

Example 1

mysql> SELECT 30 IN(1,3,4,50,30);
+--------------------+
| 30 IN(1,3,4,50,30) |
+--------------------+
|                  1 |
+--------------------+
1 row in set (0.00 sec)

Example 2


mysql> SELECT 30 IN(1,3,4,50,31);
+--------------------+
| 30 IN(1,3,4,50,31) |
+--------------------+
|                  0 |
+--------------------+
1 row in set (0.00 sec)

Example 3

mysql> SELECT 'a' IN(1,'a','c',4,50,31);
+---------------------------+
| 'a' IN(1,'a','c',4,50,31) |
+---------------------------+
|                         1 |
+---------------------------+
1 row in set, 1 warning (0.00 sec)

Example 4

mysql> SELECT * FROM student;
+------------+------+------+
| student_id | name | age  |
+------------+------+------+
|          1 | John |   28 |
|          2 | Bob  |   30 |
|          3 | Kat  |   28 |
|          4 | Pher |   24 |
|          5 | Jim  |   21 |
+------------+------+------+
5 rows in set (0.00 sec)

mysql> SELECT * FROM student
    -> WHERE age IN(30,21);
+------------+------+------+
| student_id | name | age  |
+------------+------+------+
|          2 | Bob  |   30 |
|          5 | Jim  |   21 |
+------------+------+------+
2 rows in set (0.00 sec)

Example 5

mysql> SELECT * FROM student
    -> WHERE name IN('Jim','kat','bob','Alex');
+------------+------+------+
| student_id | name | age  |
+------------+------+------+
|          2 | Bob  |   30 |
|          3 | Kat  |   28 |
|          5 | Jim  |   21 |
+------------+------+------+
3 rows in set (0.00 sec)


Related Links



Navigation
Web
SQL
MISC
References