Mysql example of converting birth date into age and grouping and counting the number of people

Mysql example of converting birth date into age and grouping and counting the number of people

Querying the database

SELECT * FROM `student`

Query results

id name birthday
1 Zhang San 1970-10-01
2 Li Si 1990-10-01
3 Wang Wu 2002-10-01
4 Ma Liu 2003-10-01

Convert to age query statement, use the TIMESTAMPDIFF function

SELECT id, name, birthday, TIMESTAMPDIFF(YEAR,student.birthday,CURDATE()) as age FROM `student`

Query results

d name birthady age
1 Zhang San 1970-10-01 50
2 Li Si 1990-10-01 30
3 Wang Wu 2002-10-01 18
4 Ma Liu 2003-10-01 17

Then group them by age group

SELECT
	GROUP_CONCAT(a.id) as ids,
	GROUP_CONCAT(a.name) as names,
	CASE
		WHEN a.age<18 THEN 'teen'
		WHEN a.age>=18 AND a.age< 40 THEN 'youth'
		WHEN a.age>=40 AND a.age< 60 THEN 'middle-aged'
	END as age,
	count(a.id) as count
FROM
	(SELECT id, name, birthday, TIMESTAMPDIFF(YEAR,student.birthday,CURDATE()) as age FROM `student`) as a
GROUP BY
	CASE
		WHEN a.age<18 THEN 'teen'
		WHEN a.age>=18 AND a.age< 40 THEN 'youth'
		WHEN a.age>=40 AND a.age< 60 THEN 'middle-aged'
	END

Query results

ids names age count
1 Zhang San middle aged 1
4 Ma Liu juvenile 1
2,3 Li Si , Wang Wu youth 2

Tips: The current time is 2020

Summarize

This is the end of this article about converting MySQL date of birth to age and grouping and counting the number of people. For more related MySQL date of birth to age and grouping and counting content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Postgresql operation of getting age by date of birth
  • Multiple ways to calculate age by birthday in MySQL
  • SQL example code for judging age by date function

<<:  Implement 24+ array methods in JavaScript by hand

>>:  UDP simple server client code example

Recommend

A brief discussion on this.$store.state.xx.xx in Vue

Table of contents Vue this.$store.state.xx.xx Get...

How to install ROS Noetic in Ubuntu 20.04

Disclaimer: Since the project requires the use of...

Detailed explanation of MySQL instance with SSD storage enabled

Detailed explanation of MySQL instance with SSD s...

Use JS to zoom in and out when you put the mouse on the image

Use JS to zoom in and out when the mouse is on th...

Use of MySQL SHOW STATUS statement

To do MySQL performance adjustment and service st...

How to build a new image based on an existing image in Docker

Building new images from existing images is done ...

Specific use of Linux which command

We often want to find a file in Linux, but we don...

Deleting two images with the same id in docker

When I created a Docker container today, I accide...

JS Canvas interface and animation effects

Table of contents Overview Canvas API: Drawing Gr...

Explore the truth behind the reload process in Nginx

Today's article mainly introduces the reload ...

How to use geoip to restrict regions in nginx

This blog is a work note environment: nginx versi...

MySQL complete collapse query regular matching detailed explanation

Overview In the previous chapter, we learned abou...