I’ve got a MySQL Table with the following structure
+----+----------+---------+
| id | incident | message |
+----+----------+---------+
| 1 | 3 | 1 |
| 2 | 3 | 2 |
| 3 | 5 | 1 |
| 4 | 6 | 0 |
| 5 | 6 | 4 |
| 6 | 6 | 1 |
| 7 | 7 | 1 |
| 8 | 7 | 2 |
+----+----------+---------+
This is simplified data, but it’s the same thing.
What I want is GROUP BY incident but only fetch those rows with the highest message value.
So taken the data above I want to fetch the following ids: 2, 3, 5, 8
I did a GROUP BY incident ORDER BY message statement, but that didn’t work. I’m not very familiar with the grouping stuff in MySQL, so it would be great if you can help me out here.
The query has subquery which gets the maximum
messagefor eachincident. The result of it is then join against theoriginal table itself based on two conditions, that it matched with theincidentand themessage