I have 2 tables
NAME
+----+---------+
| id | name |
+----+---------+
| 1 | ABC |
| 2 | CCC |
+----+---------+
TBL_STATUS
+----+-----------+--------+
| id | name_id | status |
+----+-----------+--------+
| 1 | 1 | KU |
| 2 | 1 | HP |
| 3 | 1 | HK |
| 4 | 2 | KU |
| 5 | 2 | HP |
+----+-----------+--------+
I select and join together (INNER JOIN) this 2 tables.
This is the result for my query.
+---------+--------+
| name | status |
+---------+--------+
| ABC | HK |
| ABC | HP |
| ABC | KU |
| CCC | HP |
| CCC | KU |
+---------+--------+
Each name can have 3 status. HK, HP, or KU.
I want only a row for each name for the latest status.
For example:
+---------+--------+
| name | status |
+---------+--------+
| ABC | HK |
| CCC | HP |
+---------+--------+
Status priority should display with HK first (if any),
then if no HK display HP, and so on..
You are on the right tracks with your final output of
you just need to limit this to the max ID for each name, which is achieved in the below using an
INNER JOINon an aggregate query.