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

Abbreviation of HTML DOCTYPE

If your DOCTYPE is as follows: Copy code The code ...

Solve the problem of OpenLayers 3 loading vector map source

1. Vector Map Vector graphics use straight lines ...

Native JS to achieve drag photo wall

This article shares with you a draggable photo wa...

How to use mysqladmin to get the current TPS and QPS of a MySQL instance

mysqladmin is an official mysql client program th...

JavaScript imitates Xiaomi carousel effect

This article is a self-written imitation of the X...

Example code for implementing the wavy water ball effect using CSS

Today I learned a new CSS special effect, the wav...

Installation and use of mysql on Ubuntu (general version)

Regardless of which version of Ubuntu, installing...

Teach you how to subcontract uniapp and mini-programs (pictures and text)

Table of contents 1. Mini Program Subcontracting ...

Practical TypeScript tips you may not know

Table of contents Preface Function Overloading Ma...

This article takes you into the world of js data types and data structures

Table of contents 1. What is dynamic typing? 2. D...

How to use SessionStorage and LocalStorage in Javascript

Table of contents Preface Introduction to Session...

How to generate mysql primary key id (self-increment, unique and irregular)

Table of contents 1. Use the uuid function to gen...

Methods and steps for deploying multiple war packages in Tomcat

1 Background JDK1.8-u181 and Tomcat8.5.53 were in...

Install MySQL (including utf8) using Docker on Windows/Mac

Table of contents 1. Docker installation on Mac 2...

SQL implementation of LeetCode (197. Rising temperature)

[LeetCode] 197.Rising Temperature Given a Weather...