Mysql Date Ranges

Ads
 

Mysql Date Ranges

Mysql Date Ranges is useful when you want to see the records between two dates.

Mysql Date Ranges is useful when you want to see the records between two dates.

Mysql Date Ranges

     

Mysql Date Ranges is useful when you want to see the records between two dates.

Understand with Example

The Tutorial grasps you the elaborative example from 'Mysql Date Ranges'. To understand this example we create a table 'userform' with required fieldnames and datatypes respectively. The table 'userform' has a Primary Key Id. 

Query to create table "userform":

CREATE TABLE `userform` ( 
`ID` int(11) NOT NULL auto_increment, 
`username` varchar(100) default NULL, 
`fname` varchar(100) default NULL, 
`email` varchar(100) default NULL, 
`today` date default NULL, 
PRIMARY KEY (`ID`), 
KEY `date_index` (`today`) 
)

Query to insert the record in the table "userform":

The Query insert into is used to add the records or rows to the table "userform".

insert into userform (Id,username,fname,email,today) values (1,'vineet','vineet','[email protected]','2008-12-08');
insert into userform (Id,username,fname,email,today) values (1,'saurabh','saurabh','[email protected],'2008-12-18');
insert into userform (Id,username,fname,email,today) values (1,'amit','Amit','[email protected]','2008-12-08');
insert into userform (Id,username,fname,email,today) values (1,'suman','Suman','[email protected],'2009-01-02');
insert into userform (Id,username,fname,email,today) values (1,'amar','Amar','[email protected]','2009-01-08');


Query to show the records between two dates 

The Query is used to return the records between the two dates specified in Where Clause from the table 'userform'. 

mysql> select * from userform where today>='2008-12-08' and today <='2009-01-0
+----+----------+---------+------------------------+------------+
| ID | username | fname | email | today |
+----+----------+---------+------------------------+------------+
| 1 | vineet | vineet | [email protected] | 2008-12-08 |
| 2 | saurabh | saurabh | [email protected] | 2008-12-18 |
| 3 | amit | Amit | [email protected] | 2009-01-01 |
| 4 | suman | Suman | [email protected] | 2009-01-02 |
| 5 | amar | Amar | [email protected] | 2009-01-08 |
+----+----------+---------+------------------------+------------+
5 rows in set (0.00 sec)

Ads

Ads