Kevin

天高任鸟飞,海阔凭鱼跃

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  23 随笔 :: 0 文章 :: 39 评论 :: 0 引用

两张表:
    Tongs
TongId    int
Title     nvarchar(50)
TypeId    int

    TongType
TypeId    int
TypeName  nvarchar(20)


取出某一类别下前n条记录可用:
select top n TongId,Title from Tongs where TypeId = @TypeId

但我希望一次取出所有类别里的前n条记录
不用存储过程

我自己想到的一个解决办法是:
在Tongs表中置一标识位
    Tongs
TongId    int
Title     nvarchar(50)
TypeId    int
IsShow    bool

select top n TongId,Title from Tongs where IsShow=1 order by TypeId,TongId desc

在插入新记录时,我把插入前当前类别下通过上面的select语句选出的第n条语句的IsShow置0



参考:

http://community.csdn.net/Expert/topic/4234/4234286.xml?temp=.5002558


高手的方法

select
    a.*
from
    Tongs a
where
    a.TongId in(select top N TongId from Tongs where TypeId=a.TypeId order by TongId desc)

posted on 2006-03-14 19:48 Kevin 阅读(132) 评论(1) 编辑 收藏

评论

#1楼 2008-08-15 10:22 hateyoucode      
select
a.*
from
Tongs a
where
a.TongId in(select top N TongId from Tongs where TypeId=a.TypeId order by TongId desc)

这个你试过没,我感觉会丢失类,也就是说有的TypeId的记录没出来,本来有3个类别的,但根据N的不同,有是后出来2个类的记录,有时候出来3个类的记录,我不知道怎么改啊..
 回复 引用 查看