I have multiple tables product,quote,customer and i need to get only latest record from the group. I have seen following answer but this didn't work as I have multiple joins in my query Retrieving the last record in each group - MySQL
SELECT
i.id,
i.rate,
i.created_at,
i.product_id,
p.name
FROM
`quote_item` i
JOIN `quote` q ON
i.quote_id = q.id
JOIN `product` p ON
i.product_id = p.id
WHERE
q.customer_id = 1
ORDER BY
i.id
DESC
The above query gives following result
Id rate created_at product_id product_name
--------------------------------------------------------
36 450 2022-01-06 13:59:00 17 abc
23 400 2022-01-06 06:11:52 17 abc
22 400 2022-01-06 06:08:28 3 abc
21 350 2022-01-06 05:57:42 4 abc
17 150 2022-01-04 18:33:45 1 abc
3 300 2022-01-02 01:53:50 3 abc
Now when group by is used
SELECT
i.id,
i.rate,
i.created_at,
i.product_id,
p.name
FROM
`quote_item` i
left JOIN `quote` q ON
i.quote_id = q.id
left JOIN `product` p ON
i.product_id = p.id
WHERE
q.customer_id = 1
GROUP BY
p.id
ORDER BY
i.id
ASC
It gives following result
Id rate created_at product_id product_name
--------------------------------------------------------
23 400 2022-01-06 06:11:52 17 abc
21 350 2022-01-06 05:57:42 4 abc
17 150 2022-01-04 18:33:45 1 abc
3 300 2022-01-02 01:53:50 3 abc
BUT THE EXPECTED RESULT IS AS FOLLOWS
Id rate created_at product_id product_name
--------------------------------------------------------
36 450 2022-01-06 13:59:00 17 abc
21 350 2022-01-06 05:57:42 4 abc
17 150 2022-01-04 18:33:45 1 abc
3 300 2022-01-02 01:53:50 3 abc
Any help will be appreciated.