Home Sql Mysql-example MySQL range
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

MySQL range

Advertisement
May times the programmer needs to find out some specific data which lies in the particular range of values.

MySQL range

     

May times the programmer needs to find out some specific data which lies in the particular range of values but there is not any predefined function in MySQL but we can also find that particular range's data from the database in MySQL using BETWEEN and AND clauses or write your own simple query.

Syntax:

exp BETWEEN min_value AND max_value

The above syntax is equivalent to the expression (min_value <= exp AND exp <= max_value).

In the following example we will describe you that how one can find out specific data lying between some range. To fire query we must have some data into a table therefore we will first create a table and fill some data into it.

 

 Query  CREATE TABLE `mca` (
`id` bigint(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`subject` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ;


/*Data for the table `mca` */

insert into `mca`(`id`,`name`,`subject`) values (1,'Amit','Computer'), (2,'Ramesh','Computer '), (3,'Suman','Computer'),(4,'Vineet','Java'),(5,'Sandeep','C++');
 

Output

 
+----+---------+-----------+
| id | name    | subject   |
+----+---------+-----------+
|  1 | Amit    | Computer  |
|  2 | Ramesh  | Computer  |
|  3 | Suman   | Computer  |
|  4 | Vineet  | Java      |
|  5 | Sandeep | C++       |
+----+---------+-----------+

 Now suppose we want to know the persons record which lies between the id ranges from 2 to 5 then we can do this with the following query

  Query SELECT id, name, subject from mca where 
mca.id BETWEEN 2 AND 5;

Above query can also be written as below:

SELECT id, name, subject from mca where 
( mca.id >=2 && mca.id <=5);

 

Output

 
+----+---------+-----------+
| id | name    | subject   |
+----+---------+-----------+
|  2 | Ramesh  | Computer  |
|  3 | Suman   | Computer  |
|  4 | Vineet  | Java      |
|  5 | Sandeep | C++       |
+----+---------+-----------+
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 17, 2009

Related Tutorials

Ask Questions?    Discuss: MySQL range  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com