どうすればGROUP BY id_thread
id_thread
がない場合 テーブルの列?
SELECT *
FROM contacts
WHERE id_receiver = 1
--- GROUP BY id_thread
--- removed
ORDER BY created DESC
LIMIT 1 --- only show one row
コメントに基づいて、必要なのは最新のものです(created
の順に並べられています) )すべてのid_thread
の行 、これは別のより複雑なクエリです。この種のクエリには、 [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