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

推荐订阅源

量子位
S
Securelist
MyScale Blog
MyScale Blog
Jina AI
Jina AI
罗磊的独立博客
The Cloudflare Blog
美团技术团队
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
雷峰网
雷峰网
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
博客园 - 聂微东
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
T
Tailwind CSS Blog
Attack and Defense Labs
Attack and Defense Labs
博客园_首页
Latest news
Latest news
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
D
Docker
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Security Latest
Security Latest
V2EX - 技术
V2EX - 技术
N
News | PayPal Newsroom
Microsoft Security Blog
Microsoft Security 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)