 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
MySQL query to get the dates between range of records displaying student’s Date of Birth?
For fetching records between dates, use BETWEEN. Let us first create a table −
mysql> create table DemoTable863(StudentDateOfBirth date); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable863 values('1998-01-10'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable863 values('2000-10-15'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable863 values('2003-04-20'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable863 values('2005-12-31'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable863 values('1999-07-01'); Query OK, 1 row affected (0.27 sec) Display all records from the table using select statement −
mysql> select *from DemoTable863;
This will produce the following output −
+--------------------+ | StudentDateOfBirth | +--------------------+ | 1998-01-10 | | 2000-10-15 | | 2003-04-20 | | 2005-12-31 | | 1999-07-01 | +--------------------+ 5 rows in set (0.00 sec)
Following is the query to displays dates in a range based on DOB i.e. 20 AND 21 −
mysql> select *from DemoTable863 where (YEAR(NOW()) - YEAR(StudentDateOfBirth)) BETWEEN 20 AND 21;
This will produce the following output −
+--------------------+ | StudentDateOfBirth | +--------------------+ | 1998-01-10 | | 1999-07-01 | +--------------------+ 2 rows in set (0.03 sec)
Advertisements
 