Bagaimana Anda bisa GROUP BY id_thread
ketika tidak ada id_thread
kolom di tabel Anda?
SELECT *
FROM contacts
WHERE id_receiver = 1
--- GROUP BY id_thread
--- removed
ORDER BY created DESC
LIMIT 1 --- only show one row
Berdasarkan komentar Anda, yang Anda inginkan adalah yang terbaru (diurutkan berdasarkan created
) baris untuk setiap id_thread
, yang merupakan kueri yang berbeda dan lebih kompleks. Bahkan ada tag untuk kueri semacam ini, bernama [greatest-n-per-group]
.
SELECT c.*
FROM contacts AS c
JOIN
( SELECT id_thread, MAX(created) AS created
FROM contacts
WHERE id_receiver = 1
GROUP BY id_thread
) AS g
ON (g.id_thread, g.created) = (c.id_thread, c.created)
WHERE c.id_receiver = 1