Order By before Group By doesn’t work in Symfony 4( I know it’s duplicate but didn’t find the solution )

I am gonna sort the data by date first. And then do the group by in Symfony. But it doesn’t sort the data and it returns every first row as a unique that are grouped by.
I can see the same issue here

But the resolved answer doesn’t work for me:

SELECT n.id, n.update_date
FROM (
    SELECT o.id, o.update_date
    FROM orders o 
    ORDER BY o.update_date DESC
) n 
GROUP BY n.id;

Let’s say my data should look like this:

id name update_date
1  Ali   2020/2/14
2  Luiz  2019/4/5
3  Ali   2020/4/18

What I expect to get:

id name update_date
2  Luiz  2019/4/5
3  Ali   2020/4/18

But I get this:

id name update_date
1  Ali   2020/2/14
2  Luiz  2019/4/5

This means, the data is not sorted by date.

Here’s what I tried:

SELECT *
FROM (
    SELECT *
    FROM orders_table o 
    ORDER BY o.update_date DESC
) n 
GROUP BY n.name;

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *