Fetch a value between different values in MySQL

MySQLMySQLi Database

Use MySQL BETWEEN to fetch a value between different values. Let us first create a table −

mysql> create table DemoTable1473
   -> (
   -> EmployeeCode varchar(20)
   -> );
Query OK, 0 rows affected (0.47 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1473 values('EMP_120');
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable1473 values('EMP_125');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1473 values('EMP_30');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1473 values('EMP_130');
Query OK, 1 row affected (0.10 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1473;

This will produce the following output −

+--------------+
| EmployeeCode |
+--------------+
| EMP_120      |
| EMP_125      |
| EMP_30       |
| EMP_130      |
+--------------+
4 rows in set (0.00 sec)

Here is the query to fetch a value between different values −

mysql> select * from DemoTable1473
   -> where EmployeeCode between 'EMP_124' and 'EMP_129';

This will produce the following output −

+--------------+
| EmployeeCode |
+--------------+
| EMP_125      |
+--------------+
1 row in set (0.00 sec)
raja
Published on 10-Dec-2019 07:43:33
Advertisements