- Nuxt 4项目结构的调整
- .NET版本FCKeditor2.6.4的使用方法
- ASP.NET中Cookies的用法
- 在vs2005中用sql语句访问access数据库出现replace函数未定义错误
- CSS中的“>”符号作用
- 四步解析站长如何快速提高网站权重
- C#用的是decimal类型读取货币类型数据取2位小数
- uni-app开发小程序时@tap和click的区别
- 主题:jQuery语法总结和注意事项(节摘)
- .net中Web.config文件的基本原理及相关设置
邮箱:
手机: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语句写法
-
2013-11-22移动控件介绍及详细使用方法:Image控件
-
2019-12-05淘宝运营和京东运营有什么区别
-
2019-11-24C#判断字符串中是否包含指定字符串及contains与indexof方法效率问题
-
2010-08-03关于innerhtml用法
-
2023-09-13Vue3.0中ref函数
-
2010-07-2049个影响网站排名的因素(完整版)
