Jak můžete GROUP BY id_thread
když neexistuje žádné id_thread
sloupec ve vaší tabulce?
SELECT *
FROM contacts
WHERE id_receiver = 1
--- GROUP BY id_thread
--- removed
ORDER BY created DESC
LIMIT 1 --- only show one row
Na základě vašich komentářů je to, co chcete, nejnovější (seřazeno podle created
) řádek pro každý id_thread
, což je jiný a složitější dotaz. Existuje dokonce značka pro tento druh dotazů s názvem [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