日期:2014-05-17  浏览次数:20425 次

求sql 或者存储过程!<统计每个用户发表的不同种类文章数量>

--用户表
create table t_user

  userId int identity(1,1) primary key,
  userName nvarchar(10)
)
go

--文章类型表
create table t_type
(
  typeId int identity(1,1) primary key,
  typeName nvarchar(10)
)
go

--文章表
create table t_article
(
  articleId int identity(1,1) primary key,
  userId int not null,
  typeId int not null,
  articleName nvarchar(20)
  constraint [fk_article_user] foreign key (userId)  references t_user(userId),
  constraint [fk_article_type] foreign key (typeId)  references t_type(typeId)
)
go

insert into t_user values('张三') --1
insert into t_user values('李四') --2
insert into t_user values('王五') --3

insert into t_type values('新闻') --1
insert into t_type values('体育') --2
insert into t_type values('娱乐') --3

--张三(新闻3篇 ,体育2篇,娱乐1篇)
insert into t_article values(1,1,'新闻标题1')
insert into t_article values(1,1,'新闻标题2')
insert into t_article values(1,1,'新闻标题3')
insert into t_article values(1,2,'体育标题1')
insert into t_article values(1,2,'体育标题2')
insert into t_article values(1,3,'娱乐标题1')

--李四(新闻0篇,体育2篇,娱乐2篇)
insert into t_article values(2,2,'体育标题1')
insert into t_article values(2,2,'体育标题2')
insert into t_article values(2,3,'娱乐标题1')
insert into t_article values(2,3,'娱乐标题2')

--王五(无发表)

--问题:需要统计每个用户发表的不同种类文章数量,格式如下:
/*
 用户姓名 新闻类别 体育类别 娱乐类别
  张三       3        2        1
  李四       0        2        2
  王五       0        0        0
*/