的是這兩個MySQL之間的區別查詢子查詢和聯接的子查詢有什麼區別?
select t.id,
(select count(c.id) from comment c where c.topic_id = t.id) as comments_count
from topic;
和
select t.id,comments.count from topic
left join
(
select count(c.id) count,c.topic_id from comment c group by topic_id
) as comments on t.id = comments.topic_id
我知道那裏有沒有太多的信息。只是想知道什麼時候使用子查詢並加入了子查詢以及它們之間的區別。
感謝
你的第二個例子是一個派生表的連接。好處是派生表是一次生成,基於集合。第一個相關的子查詢可能會在「topic」表的每一行中執行一次 – StuartLC
所以,你的意思是子查詢可以檢索單個記錄嗎?和第二個撤回清單? – reverbnation
@reverbnation如果你想提出一個StuartLC的問題,你應該用@ @ – 2015-06-14 15:38:16