查询出每个类型中点击数为前10条的 文章,sql语句怎么写?查询的结果要如下显示:
title Column_Name coc_click//文章表
create table doc
(
id int identity(1,1) not null primary key ,
title varchar(255) not null ,//标题
state bit not null, //状态
coutent nvarchar(1000) //内容
cid int //外键 没有理上的外键关系 引用 class(Column_ID) 是逻辑上存在的关系)//文章点击数表
create table doc_count
(
id int identity(1,1) not null primary key,
doc_id int not null ,//外键 没有建立物理上的外键关系 引用 doc(id) 是逻辑上存在的关系
coc_click int not null default 0 //点击数
)//文章类型表
create table class
(
Column_ID int identity(1,1) primary key,
Column_Name varchar(50) not null //类型名称
)
title Column_Name coc_click//文章表
create table doc
(
id int identity(1,1) not null primary key ,
title varchar(255) not null ,//标题
state bit not null, //状态
coutent nvarchar(1000) //内容
cid int //外键 没有理上的外键关系 引用 class(Column_ID) 是逻辑上存在的关系)//文章点击数表
create table doc_count
(
id int identity(1,1) not null primary key,
doc_id int not null ,//外键 没有建立物理上的外键关系 引用 doc(id) 是逻辑上存在的关系
coc_click int not null default 0 //点击数
)//文章类型表
create table class
(
Column_ID int identity(1,1) primary key,
Column_Name varchar(50) not null //类型名称
)
*
FROM doc AS A
JOIN doc_count AS B
ON A.id=B.doc_id
JOIN class AS C
ON A.cid=C.Column_ID
WHERE 10>(SELECT COUNT(*)
FROM doc AS D
JOIN doc_countAS E
ON D.id=D.doc_id
JOIN class AS F
ON D.cid=F.Column_ID
WHERE F.Column_ID=C.ColumnID
AND B.doc_Click>E.doc_Click
);
用个存储过程create table #tb (title nvarchar(50),Column_Name nvarchar(50),coc_click int)
declare @cid int
declare cur_class cursor for
select distinct cid from doc
open cur_class
fetch next from cur_class into @cid
while(@@fetch_status=0)
begininsert into #tb
select top 10 doc.title,class.Column_Name,doc_Count.coc_click from doc join class on
doc.cid=class.ColumnID join doc_Count on doc.id=doc_Count.doc_id where doc.cid=@cid
fetch next from cur_class into @cid
end
close cur_class
deallocate cur_class
select * from #tb
drop table #tb
这个用group by 加having不行吗?