Should I use a loop or 'OR' operator to query a bunch of stuff at a fast pace In MYSQL?

MySQLMySQLi Database

For faster querying, you need to use MySQL IN(). Let us first create a table −

mysql> create table DemoTable1538
   -> (
   -> ClientId int,
   -> ClientName varchar(20)
   -> );
Query OK, 0 rows affected (0.59 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1538 values(101,'Chris');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1538 values(102,'Robert');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable1538 values(103,'Bob');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1538 values(104,'Adam');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1538;

This will produce the following output −

+----------+------------+
| ClientId | ClientName |
+----------+------------+
|      101 | Chris      |
|      102 | Robert     |
|      103 | Bob        |
|      104 | Adam       |
+----------+------------+
4 rows in set (0.00 sec)

Following show we can query multiple values quickly −

mysql> select * from DemoTable1538 where ClientId IN(101,103,104);

This will produce the following output −

+----------+------------+
| ClientId | ClientName |
+----------+------------+
|      101 | Chris      |
|      103 | Bob        |
|      104 | Adam       |
+----------+------------+
3 rows in set (0.00 sec)
raja
Updated on 12-Dec-2019 05:50:49

Advertisements