Mysql version: 8.0.21
I am lookig of get the latest value of each "TableData" which has the type "fruit".
Table Name: TableNames
_________________________________________
| id | name | id_group | type |
|-----------------------------------------|
| 0 | AppleGroup | apple | fruit |
| 1 | BananaGroup | banana | fruit |
| 2 | OtherGroup | other | other |
Table Name: TableData
__________________________
| id | id_group | value |
|--------------------------|
| 0 | apple | 12 |
| 1 | banana | 8 |
| 2 | apple | 3 | <--get latest
| 3 | banana | 14 |
| 4 | banana | 4 | <--get latest
With this Query I get all the items, but I am looking for the lastest of each.
I already tried to group by and order by, but the problem is that I first need to order by and then group by, seems that’s not possible in Mysql.
SELECT
n.name,
d.value
FROM TableNames n
INNER JOIN
(
SELECT *
FROM TableData
) d ON d.`id_group` = n.`id_group`
WHERE type = 'fruit'
Expected ouput:
_____________________
| name | value |
|---------------------|
| AppleGroup | 3 |
| BananaGroup | 4 |
2
Answers
On MySQL 8+, we can use
ROW_NUMBER()
:For optimization, you may consider adding the following index to the
TableData
table:Note that on InnoDB, MySQL will automatically include
id
at the end of the index, hence the index is(id_group, id)
. This should let MySQL efficiently do the join in the CTE and also computeROW_NUMBER
.Without
ROW_NUMBER()
, because you can be on an older version of MySQL (before 8.0), you can create an inner join with the max(id):see: DBFIDDLE