0

我有两个表,博客表有一个指向 BlogTag 表的 FK BlogTagID 列:

博客表:

BlogID BlogTagID BlogTitle
  1       2        test1
  2       1        test2
  3       2        test3

博客标签表:

BlogTagID BlogTagName
  1        JAVA        
  2        .NET        
  3        PHP       

我想得到结果:

BlogTagName  count
   JAVA         1
   .NET         2
   PHP          0

如何得到这个?非常感谢!

4

2 回答 2

2

试试这个代码

select BlogTagName, count(blogid)
from BlogTag bt
left join Blog b on b.blogtagid = bt.BlogTagID
group by BlogTagName

SQL FIDDLE:http ://sqlfiddle.com/#!3/356c5/8/0

于 2013-10-02T02:50:29.527 回答
1

你也可以试试这个

SELECT BlogTagName,COUNT(BlogTagID) FROM Blog b JOIN BlogTagID bt WHERE b.BlogTagID=bt.BlogTagID GROUP BY BlogTagID;
于 2013-10-02T02:52:16.257 回答