MySQL query to check if multiple rows exist?


Let us first create a table −

mysql> create table DemoTable1219
(
   Id int,
   Name varchar(40)
);
Query OK, 0 rows affected (0.43 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1219 values(100,'Adam');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1219 values(101,'John');
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable1219 values(102,'Chris');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1219 values(103,'Bob');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable1219;

This will produce the following output −

+------+-------+
| Id   | Name  |
+------+-------+
|  100 | Adam  |
|  101 | John  |
|  102 | Chris |
|  103 | Bob   |
+------+-------+
4 rows in set (0.00 sec)

Following is the query to check if multiple rows exist −

mysql> select count(distinct Id)=3 from DemoTable1219 where Id IN(100,103,102);

This will produce the following output −

+----------------------+
| count(distinct Id)=3 |
+----------------------+
|                   1 |
+----------------------+
1 row in set (0.00 sec)

Note − The returned value is 1 that means multiple rows exists.

raja
Published on 07-Oct-2019 15:58:52
Advertisements