Select last result of every parent within it's own table

I have a table.

This table has following columns:

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.

something like this?

SELECT parent
     , MAX(id) AS highestID
  FROM tableName
  GROUP BY parent
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

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.