
- 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
Can I get Age using BirthDate column in a MySQL query?
To get Age using BirthDate column in a MySQL query, you can use datediff(). Let us first create a table:
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, DateOfBirth date ); Query OK, 0 rows affected (1.46 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into DemoTable(DateOfBirth) values('2010-01-21'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(DateOfBirth) values('1993-04-02'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(DateOfBirth) values('1999-12-01'); Query OK, 1 row affected (1.53 sec) mysql> insert into DemoTable(DateOfBirth) values('1998-11-16'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(DateOfBirth) values('2004-03-19'); Query OK, 1 row affected (0.11 sec)
Following is the query to display records from the table using select command:
mysql> select *from DemoTable;
This will produce the following output:
+----+-------------+ | Id | DateOfBirth | +----+-------------+ | 1 | 2010-01-21 | | 2 | 1993-04-02 | | 3 | 1999-12-01 | | 4 | 1998-11-16 | | 5 | 2004-03-19 | +----+-------------+ 5 rows in set (0.00 sec)
Let us now get Age using birthdate column with DATEDIFF() method:
mysql> select cast(DATEDIFF(curdate(),DateOfBirth) / 365.25 AS UNSIGNED) AS AGE from DemoTable;
This will produce the following output:
+------+ | AGE | +------+ | 9 | | 26 | | 19 | | 20 | | 15 | +------+ 5 rows in set (0.00 sec)
Let us now write a query to get the DOB and ID where Age is between 20 and 26:
mysql> SELECT *FROM DemoTable WHERE YEAR(CURDATE())-YEAR(DateOfBirth) BETWEEN 20 AND 26;
This will produce the following output:
+----+-------------+ | Id | DateOfBirth | +----+-------------+ | 2 | 1993-04-02 | | 3 | 1999-12-01 | | 4 | 1998-11-16 | +----+-------------+ 3 rows in set (0.00 sec)
- Related Articles
- How to calculate age in years from birthdate in MySQL?
- Can I search for particular numbers in a MySQL column with comma separated records using a MySQL query?
- How can I get maximum and minimum values in a single MySQL query?
- Can I get the count of repeated values in a column with MySQL?
- How can I get the output of multiple MySQL tables from a single query?
- How can I stop a running MySQL query?
- How can I stop running a MySQL query?
- MySQL query to get the count of distinct records in a column
- Get multiple count in a single MySQL query for specific column values
- How to get a specific column record from SELECT query in MySQL?
- How can I get enum possible values in a MySQL database using PHP?
- Divide a column to get monthly salary of employees in a MySQL Query?
- How can I get the number of times a specific word appears in a column with MySQL?
- Using MySQL, can I sort a column but allow 0 to come last?
- How can I remove every column in a table in MySQL?

Advertisements