理解 sql 中exists 子查询
来源:互联网 发布:js对url编码 编辑:程序博客网 时间:2024/05/21 19:39
最近在一个项目中有这样一个需求,在一个表A(id,num,adddate) 中 查询出adddate 时间间隔相差两秒以内的记录!最后还是到csdn上求助(http://topic.csdn.net/u/20080509/16/786bcc61-5697-4762-be17-8d4c5955c28e.html) 问题才解决,sql语句如下
--建立测试环境
set nocount on
create table test(ID varchar(20),Num varchar(20),AddDate varchar(20))
insert into test select '1','5','2008-03-10 9:26:20'
insert into test select '2','2','2008-03-10 9:26:22'
insert into test select '3','2','2008-03-10 9:28:20'
insert into test select '4','2','2008-03-15 10:26:20'
insert into test select '5','6','2008-03-15 10:26:22'
go
--测试
select * from test a
where exists(select 1 from test where id<>a.id
and abs(datediff(s,a.AddDate,AddDate))<=2)
--删除测试环境
drop table test
set nocount off
/**//*
1 5 2008-03-10 9:26:20
2 2 2008-03-10 9:26:22
4 2 2008-03-15 10:26:20
5 6 2008-03-15 10:26:22
*/
set nocount on
create table test(ID varchar(20),Num varchar(20),AddDate varchar(20))
insert into test select '1','5','2008-03-10 9:26:20'
insert into test select '2','2','2008-03-10 9:26:22'
insert into test select '3','2','2008-03-10 9:28:20'
insert into test select '4','2','2008-03-15 10:26:20'
insert into test select '5','6','2008-03-15 10:26:22'
go
--测试
select * from test a
where exists(select 1 from test where id<>a.id
and abs(datediff(s,a.AddDate,AddDate))<=2)
--删除测试环境
drop table test
set nocount off
/**//*
1 5 2008-03-10 9:26:20
2 2 2008-03-10 9:26:22
4 2 2008-03-15 10:26:20
5 6 2008-03-15 10:26:22
*/
首先还感谢jinjazz 的帮助,看到这个语句,发觉自己对exists 子查询不能理解,以前对exists的使用也就是判断某个条件的记录是否存在。然后查找sql2000 帮助
EXISTS
指定一个子查询,检测行的存在。
语法
EXISTS subquery
参数
subquery
是一个受限的 SELECT 语句 (不允许有 COMPUTE 子句和 INTO 关键字)。有关更多信息,请参见 SELECT 中有关子查询的讨论。
结果类型
Boolean
结果值
如果子查询包含行,则返回 TRUE。
请理解的朋友帮忙解释一下!
- 理解 sql 中exists 子查询
- 理解exists子查询
- SQL 子查询 EXISTS 和 NOT EXISTS
- Sql - EXISTS 用于子查询
- mysql中exists子查询
- 理解SQL语句中 Exists()
- 相关子查询中exists后select 加数字的理解
- 子查询(exists子查询)
- MSSQL EXISTS子查询
- MySQL 子查询-exists
- EXISTS子查询
- SQL中关于EXISTS谓词的理解
- SQL中行为不当的子查询,IN,exists
- SQL关于相关子查询与EXISTS的简单总结
- mysql子查询 -exists,not exists
- MySQL 子查询 EXISTS 和 NOT EXISTS
- 如何理解exists sql
- mysql 之 exists子查询
- 用Flock 写 CSDN 博客
- 在console模式下实现密码的输入
- weblucene实现搜索引擎(续)
- span和div的区别
- 网络教程下载地址大全
- 理解 sql 中exists 子查询
- 写注释的宏,vs6.0和vs2003两个版本的
- 渗透(香港)劲舞团官方论坛
- Create the Link
- 蔡军生先生第二人生的源码分析(十四)人物角色的实现
- Java输入输出流
- 虚拟网络
- 用别人的标准要求别人
- 信息安全移动与集成研究资料共享:Office SharePoint Server 2007 规划和体系结构