Hoe kun je GROUP BY id_thread
wanneer er geen id_thread
. is kolom in uw tabel?
SELECT *
FROM contacts
WHERE id_receiver = 1
--- GROUP BY id_thread
--- removed
ORDER BY created DESC
LIMIT 1 --- only show one row
Op basis van uw opmerkingen wilt u de nieuwste (in volgorde van created
.) ) rij voor elke id_thread
, wat een andere en complexere query is. Er is zelfs een tag voor dit soort zoekopdrachten, genaamd [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