惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
A
About on SuperTechFans
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
Netflix TechBlog - Medium
Microsoft Security Blog
Microsoft Security Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
博客园 - Franky
D
Docker
Martin Fowler
Martin Fowler
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
Last Week in AI
Last Week in AI
U
Unit 42
F
Fortinet All Blogs
H
Help Net Security
Blog — PlanetScale
Blog — PlanetScale
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
P
Proofpoint News Feed
月光博客
月光博客
G
Google Developers Blog

博客园 - Kevin

换工作了,记录一下面试情况 打工的就是打工的! 超强的希捷硬盘 在一个表里添加了一记录,怎样取得这条记录的id 发布程序到远程服务器 DNN所有表结构 web.config信息及RSA加密方式! vs2005调用js脚本方法总结 Using JavaScript to show the current time for the end user HtmlGenericControl操作meta标记 window2k3集成sp1光盘启动工具版 CodeSmith资源 华硕网站ajax应用分析-js脚本 Ajax程序设计入门 SQL Server端口更改后的数据库连接方式 windows2003server自带防火墙导致sql server2k无法访问,谁有好办法? 局域网访问access数据库 dreamweaver+asp+access+vss不该犯的错误 windows2k3server远程桌面连接,”终端服务器超出了最大允许连接数“的解决办法
如何一次性取出每个类别下的前n条记录
Kevin · 2006-03-14 · via 博客园 - Kevin

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