Selecting records within a range and with a condition set on two columns in MySQL?


For this, use where clause. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Number1 int,
   -> Number2 int
   -> );
Query OK, 0 rows affected (3.73 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(40,50);
Query OK, 1 row affected (0.60 sec)

mysql> insert into DemoTable values(100,59);
Query OK, 1 row affected (0.56 sec)

mysql> insert into DemoTable values(400,500);
Query OK, 1 row affected (0.40 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------+---------+
| Number1 | Number2 |
+---------+---------+
|      40 |      50 |
|     100 |      59 |
|     400 |     500 |
+---------+---------+
3 rows in set (0.00 sec)

Here is the query to select records with a range condition on two columns −

mysql> select *from DemoTable where Number1 >=51 and Number2 <=1000;

This will produce the following output −

+---------+---------+
| Number1 | Number2 |
+---------+---------+
|     100 |      59 |
|     400 |     500 |
+---------+---------+
2 rows in set (0.00 sec)
raja
Published on 10-Jul-2019 17:26:22
Advertisements