I have a web application written with PHP and MySQL.
The result of one SQL command (mentioned below) is something like this:
SELECT
t.subject, c.parent_id, c.date_time
FROM
tbl_tickets_contents c
JOIN
tbl_tickets t
ON
t.id = c.parent_id
ORDER BY
c.date_time DESC;
I want to remove duplicated items. So I changed the SQL script to this:
SELECT
t.subject, c.parent_id, c.date_time
FROM
tbl_tickets_contents c
JOIN
tbl_tickets t
ON
t.id = c.parent_id
GROUP BY
c.parent_id
ORDER BY
c.date_time DESC;
I assumed this change will give me this result:
But the output was:
It seems GROUP BY changes ORDER BY sorting order. I wanted to have the records with newer date_time
, but after this change I have records with older date_time
!
How can I solve this problem? What is my mistake?