
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- MongoDB query to get date records in a range
- MySQL query to get the current date from the list of dates
- MySQL query to select all data between range of two dates?
- Select the date records between two dates in MySQL
- Get the SUM of records between two given dates in MySQL
- MySQL date comparison to fetch dates between a given range?
- MySQL query to get the current date records wherein one of the columns displays current date
- How to display the day name on the basis of Date of Birth records in MySQL?
- MySQL query to fetch records from a range of months?
- MySQL query to order and display difference between dates from the current date
- MySQL query to retrieve current date from a list of dates
- How to get the difference between date records and the current date in MySQL?
- MongoDB query to fetch date records (ISODate format) in a range
- MySQL query to get the count of distinct records in a column
- MySQL query to fetch the latest date from a table with date records

Advertisements