Implement group by based on MySQL to get the latest data of each group

Implement group by based on MySQL to get the latest data of each group

Preface:

The group by function retrieves the first piece of data in the group, but sometimes we need to retrieve the latest piece of data in each group. How can we achieve this?

This article provides two implementation methods.

1. Prepare data

http://note.youdao.com/noteshare?id=dba748092a619be0a8f160ccf6e25a5f&sub=FD4C1C7823CA440DB360FEA3B4A905CD

Two, three implementation methods

1) Order by first and then group:

SELECT * FROM (SELECT * from tb_dept ORDER BY id descLIMIT 10000) a GROUP BY parent_id;

Not adding LIMIT may be invalid due to MySQL version issues. But I always feel that this way of writing is not very serious, because if the amount of data is greater than the Limit value, the result will be inaccurate. So there is a second way of writing.

2) Using the max() function:

SELECT * FROM tb_dept td,(SELECT max(id) id FROM tb_dept GROUP BY parent_id) md where td.id = md.id;

3) Use the where field name in (...) function:

SELECT * FROM tb_dept WHERE id IN (SELECT MAX(id) FROM tb_dept GROUP BY parent_id);

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the group by statement in MySQL database group query
  • Detailed explanation of group by and having in MySQL
  • MySQL efficient query left join and group by (plus index)
  • Detailed explanation of MySQL Group by optimization
  • How to optimize MySQL group by statement
  • How to use MySQL group by and order by together
  • mysql group by grouping multiple fields
  • A brief discussion on group by in MySQL

<<:  Common ways to optimize Docker image size

>>:  Detailed explanation of using Baidu style in eslint in React project

Recommend

Vue Learning - VueRouter Routing Basics

Table of contents 1. VueRouter 1. Description 2. ...

A brief discussion on Flex layout and scaling calculation

1. Introduction to Flex Layout Flex is the abbrev...

Causes and solutions for slow MySQL query speed and poor performance

1. What affects database query speed? 1.1 Four fa...

Zabbix monitors the process of Linux system services

Zabbix automatically discovers rules to monitor s...

Detailed explanation of the difference between chown and chmod commands in Linux

In Linux system, both chmod and chown commands ca...

Python MySQL database table modification and query

Python connects to MySQL to modify and query data...

How to view the running time of MySQL statements through Query Profiler

The previous article introduced two methods to ch...

A brief analysis of JS original value and reference value issues

Primitive values ​​-> primitive types Number S...

JS implementation of carousel example

This article shares the specific code of JS to im...

Example code of Vue3 encapsulated magnifying glass component

Table of contents Component Infrastructure Purpos...