How to achieve unique results

im looking for a request to load and group data

I have a 1: n parent: child relationship

and I want to pull the last child out of the parent, not the children,

means that each record must have a unique parent with the last child.

Table for children alt text

Desired Results alt text

I tried:

I tried the following query, but it got the oldest results

SELECT c.* FROM child  AS c GROUP BY c.parent_id HAVING(MAX(c.order))

Thanks in advance

+3
source share
1 answer
select ct.*
    from ChildTable ct
        inner join (select parent_id, max(order) as MaxOrder
                        from ChildTable
                        group by parent_id) q
            on ct.parent_id = q.parent_id
                and ct.order= q.MaxOrder
+1
source

Source: https://habr.com/ru/post/1785947/


All Articles