求与当前时间一前一后的记录的sql
时间:2011-12-25
来源:互联网
1 6:00
2 7:00
3 8:00
如果现在是7:30,则返回第2,3条记录
请不使用union all语句哦,非常感谢!
作者: zimu312500 发布时间: 2011-12-25
作者: zimu312500 发布时间: 2011-12-25
create table tb(id int,[time] time) insert into tb select 1,'6:00' insert into tb select 2,'7:00' insert into tb select 3,'8:00' insert into tb select 4,'9:00' go declare @s time set @s='7:30' select * from tb a where time>@s and not exists(select 1 from tb where time<a.time and time>@s) or time<@s and not exists(select 1 from tb where time>a.time and time<@s) /* id time ----------- ---------------- 2 07:00:00.0000000 3 08:00:00.0000000 (2 行受影响) */ go drop table tb
作者: qianjin036a 发布时间: 2011-12-25
SQL code
select top 1 * from tb where time < '7:30' order by time desc select top 1 * from tb where time > '7:30' order by time
作者: dawugui 发布时间: 2011-12-25
作者: qianjin036a 发布时间: 2011-12-25
--如果是sql server则如下:
SQL code
select top 1 * from tb where time < '7:30' order by time desc
select top 1 * from tb where time > '7:30' order by time
大乌龟大大的用了union all了,不过用这样可能效率反而会比晴天写的会高一些哦,哈哈
作者: zimu312500 发布时间: 2011-12-25
作者: qianjin036a 发布时间: 2011-12-25
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28