- 从零开始学习jQuery (十) jQueryUI常用功能实战
- 去除HTML代码的类
- SEO基础知识教程, 其实也是很重要的
- 锚文本技巧
- 一段自适应高度的圆角css矩形
- Asp.net页面中如何镶嵌Word文档
- robots文件的写法解析
- 网站目录结构、URL如何设计,更有利于搜索引擎?
- 网站推广实战:100条不容错过的经验
- 菜鸟之系统建模经验之谈:"机房收费系统"三层架构
邮箱:
手机:15383239821
取每组前几条记录的SQL写法
对于表test2(id是主键),有:
SELECT [id], [title], [typeid], [datetime] FROM [xahh].[dbo].[test2]
id title typeid datetime
1 1.1 1 1
2 1.2 1 2
3 1.3 1 3
4 2.1 2 4
5 2.2 2 5
6 2.3 2 6
7 3.1 3 7
8 3.2 3 8
9 3.3 3 9
取每个typeid的最大datetime的2条:
第一种取法:
select * from test2 a where
(select count(*) from test2 b where b.typeid = a.typeid and a.datetime< b.datetime) <=1
order by typeid, datetime desc
[解释:相同typeid的记录中比该记录datetime小的记录数不能大于1,可以保证该记录在前2条]
结果
id title typeid datetime
3 1.3 1 3
2 1.2 1 2
6 2.3 2 6
5 2.2 2 5
9 3.3 3 9
8 3.2 3 8
第二种取法:
select * from test2 a where a.id in
(select top 2 b.id from test2 b where b.typeid = a.typeid order by datetime desc)
order by typeid, datetime desc
结果
id title typeid datetime
3 1.3 1 3
2 1.2 1 2
6 2.3 2 6
5 2.2 2 5
9 3.3 3 9
8 3.2 3 8
PS:不知道有没有不用子查询的。
- 上一篇:关系型数据库性能优化总结
- 下一篇:mysql防注入的sql语句写法
-
2012-06-30没有数据分析何谈SEO?
-
2023-10-09Volist标签
-
2011-09-27sql server 2008 代理服务提供的凭据无效,sql2008安装问题
-
2023-07-30mysql使用utf8mb4经验总结
-
2011-04-07数据库中主键和外键的设计原则
-
2010-12-09URL标准化是什么意思?
