MySQL - Show the last record of each group from a Table
Show the last record of each group from a Table
You have one table class
id | name | subject | score |
---|---|---|---|
1 | Anna | Math | 10 |
2 | Anna | CS | 9 |
3 | Anna | Geo | 4 |
4 | Will | Geo | 5 |
5 | Will | CS | 8 |
6 | Jack | Geo | 10 |
7 | Jack | Math | 9 |
8 | Jill | CS | 6 |
And you want the result to be like this:
id | name | subject | score |
---|---|---|---|
3 | Anna | Geo | 4 |
5 | Will | CS | 8 |
7 | Jack | Math | 9 |
8 | Jill | CS | 6 |
Run
SELECT
c1.*
FROM
class c1
LEFT JOIN class c2 ON (c1.name = c2.name
AND c1.id < c2.id)
WHERE
c2.id IS NULL;
Need a good GUI Tool for MySQL? TablePlus is a modern, native tool with an elegant UI that allows you to simultaneously manage multiple databases such as MySQL, PostgreSQL, SQLite, Microsoft SQL Server and more.
Not on Mac? Download TablePlus for Windows.
On Linux? Download TablePlus for Linux
Need a quick edit on the go? Download TablePlus for iOS.