

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to 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)
- Related Questions & Answers
- In MySQL how to replace all NULL values in a particular field of a particular table?
- Auto insert values into a MySQL table in a range?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- MySQL Select IN range?
- MySQL query to select records from a table on the basis of a particular month number?
- Select and insert values with preceding zeros in a MySQL table
- How can we update MySQL table after removing a particular string from the values of column?
- MySQL query to select records with a particular date?
- How to replace values of select return in MySQL?
- How to check table status of the tables in a particular MySQL database?
- How to get the values of a particular row in a table in Selenium with python?
- Select specific rows in a range with MySQL?
- How to find a particular varchar id in MySQL from a list of values?
- How can we create a MySQL view by selecting some range of values from a base table?
- MySQL query to select all entries from a particular month
Advertisements