我有一个Threads包含字段(id)的表()。我想从中选择每一行Threads,以及表Posts中该字段Posts.thread与相同的行数Threads.id。
Threads
id
Posts
Posts.thread
Threads.id
如何在SQL中完成?
(像这样的伪SQL: SELECT *, COUNT(* FROM Posts WHERE Posts.id=Threads.id) FROM Threads)
SELECT *, COUNT(* FROM Posts WHERE Posts.id=Threads.id) FROM Threads
SELECT t.id, COUNT(p.thread) FROM Threads AS t LEFT OUTER JOIN Posts AS p ON t.id = p.thread GROUP BY t.id