
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Select Particular Range of Values in a MySQL Table
In order to select particular range of values in a MySQL table, you can use WHERE clause. Let us first create a table:
mysql> create table DemoTable ( CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, CustomerName varchar(200), CustomerAge int, isRegularCustomer bool ); Query OK, 0 rows affected (0.57 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into DemoTable(CustomerName,CustomerAge,isRegularCustomer)values('Chris',24,true); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(CustomerName,CustomerAge,isRegularCustomer)values('Robert',26,false); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(CustomerName,CustomerAge,isRegularCustomer)values('Mike',27,false); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(CustomerName,CustomerAge,isRegularCustomer)values('Larry',22,true); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(CustomerName,CustomerAge,isRegularCustomer)values('Sam',30,true); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(CustomerName,CustomerAge,isRegularCustomer)values('Carol',26,true); Query OK, 1 row affected (0.16 sec)
Following is the query to display records from the table using select command:
mysql> select *from DemoTable;
This will produce the following output:
+------------+--------------+-------------+-------------------+ | CustomerId | CustomerName | CustomerAge | isRegularCustomer | +------------+--------------+-------------+-------------------+ | 1 | Chris | 24 | 1 | | 2 | Robert | 26 | 0 | | 3 | Mike | 27 | 0 | | 4 | Larry | 22 | 1 | | 5 | Sam | 30 | 1 | | 6 | Carol | 26 | 1 | +------------+--------------+-------------+-------------------+ 6 rows in set (0.00 sec)
Following is the query to select particular range of values in a MySQL table. Here, we are selecting customer records with age greater than equal to 24 and less than equal to 27. Also, the customer should be a regular customer i.e. with 1 value under “isRegularCustomer” column:
mysql> select CustomerId,CustomerName,CustomerAge,isRegularCustomer from DemoTable where isRegularCustomer=true AND CustomerAge >=24 AND CustomerAge <= 27;
This will produce the following output
+------------+--------------+-------------+-------------------+ | CustomerId | CustomerName | CustomerAge | isRegularCustomer | +------------+--------------+-------------+-------------------+ | 1 | Chris | 24 | 1 | | 6 | Carol | 26 | 1 | +------------+--------------+-------------+-------------------+ 2 rows in set (0.00 sec)
Advertisements