日期:2014-05-18  浏览次数:20419 次

SQL问题
我有一个新闻类别表,其有10个类别。还有个新闻表,有各个类别的新闻。
我想查询新闻表中各个类别的前5条新闻。这该怎么弄?

------解决方案--------------------
简单写了一个.放到查询分析器里直接执行即可

try it!

SQL code

declare @type table
(
tid int,
tname varchar(50)
)
declare @news table
(
nid INT IDENTITY (1,1),
tid int,
news varchar(100)
)
insert into @type values(1,'A')
insert into @type values(2,'B')
insert into @type values(3,'C')

insert into @news values(1,'xxx1')
insert into @news values(1,'xxx2')
insert into @news values(1,'xxx3')
insert into @news values(1,'xxx4')
insert into @news values(1,'xxx5')
insert into @news values(1,'xxx6')
insert into @news values(1,'xxx7')

insert into @news values(2,'xxxa1')
insert into @news values(2,'xxxb2')
insert into @news values(2,'xxxc3')
insert into @news values(2,'xxxd4')
insert into @news values(2,'xxxe5')
insert into @news values(2,'xxxf6')
insert into @news values(2,'xxxf7')

select * from @news ns where ns.nid in (select top 5 nid from @news n where n.tid = ns.tid)