当前位置: 代码迷 >> Sql Server >> 这2个表怎么连接查询出需要的内容
  详细解决方案

这2个表怎么连接查询出需要的内容

热度:28   发布时间:2016-04-27 14:37:08.0
这2个表如何连接查询出需要的内容?
第1个表Posts
列:PostsID Title
第2个表Reply
列:ReplyID Conten PostsID(表Posts的外键)

请问:如何查出每个PostsID中的内容(即PostsID和Title列)以及对应的PostsID在表Reply的记录数。谢谢~!

------解决方案--------------------
SQL code
SELECT A.PostsID, A.Title, B.CNTFROM Posts aINNER JOIN (SELECT PostsID, COUNT(*) AS cnt FROM Reply GROUP BY PostsID) B   ON A.PostsID = B.PostsID
------解决方案--------------------
SQL code
select  a.*,b.numfrom  Posts a,  (select PostsID,count(1) as num from Reply group by PostsIDZ)bwhere  a.PostsID=b.PostsID
  相关解决方案