两张表:
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)