id (AI, unique),
name (non-important string),
parent (number between 1 and 7).
I’d like to select result with highest ID, that has parent = 1.
I’d like to select result with highest ID, that has parent = 2.
I’d like to select result with highest ID, that has parent = 3.
In single request.
I know about SELECT * FROM this WHERE parent = 1 ORDER BY id DESC.
But I would like to find the latest results for every single parent (there are 7) at once.
SELECT *
FROM topics
GROUP BY parent
ORDER BY id DESC
LIMIT 0 , 30
I’d like to select everything of these 7 results. To put it in rows.
I tried changing a bit above, but I don’t think that it actually does what I think it does.
Actually nevermind I overwent on requirements. Just listing these IDs from database is enough for me.
I thought about including a snippet like that, but you’d basically need to move the previous query into a sub query and join it in…
SELECT t1.id
, t1.name
. t1.parent
FROM tableName t1
INNER JOIN (SELECT parent
, MAX(id) AS highestID
FROM tableName
GROUP BY parent) t2 ON t1.id = t2.highestID
AND t1.parent = t2.parent