SQLServer中exists和except⽤法
⼀、exists
1.1 说明
EXISTS(包括 NOT EXISTS)⼦句的返回值是⼀个 BOOL 值。EXISTS 内部有⼀个⼦查询语句(SELECT ... ),我将其称为EXIST 的内查询语句。其内查询语句返回⼀个结果集。EXISTS ⼦句根据其内查询语句的结果集空或者⾮空,返回⼀个布尔值。exists:强调的是是否返回结果集,不要求知道返回什么,⽐如:select name from student where sex = 'm' and mark exists(select 1 from grade where ...),只要 exists 引导的⼦句有结果集返回,那么 exists 这个条件就算成⽴了,⼤家注意返回的字段始终为 1,如果改成select 2 from grade where ...,那么返回的字段就是 2,这个数字没有意义。所以 exists ⼦句不在乎返回什么,⽽是在乎是不是有结果集返回。EXISTS = IN,意思相
同不过语法上有点点区别,好像使⽤ IN 效率要差点,应该是不会执⾏索引的原因。
相对于 inner join,exists 性能要好⼀些,当它到第⼀个符合条件的记录时,就会⽴即停⽌搜索返回 TRUE。
1.2 ⽰例
--EXISTS
--SQL:
select name from family_member
where group_level > 0
and exists(select 1 from family_grade where family_member.name = family_grade.name
and grade > 90)
--result:
nameexists的用法
cherrie
--NOT EXISTS
--SQL:
select name from family_member
where group_level > 0
and not exists(select 1 from family_grade where family_member.name = family_grade.name
and grade > 90)
--result:
name
mazey
rabbit
1.3 intersect/2017-07-21
intersect 的作⽤与 exists 类似。
--intersect
-
-SQL:
select name from family_member where group_level > 0
intersect
select name from family_grade where grade > 90
--result:
name
cherrie
⼆、except
2.1 说明
查询结果上 EXCEPT = NOT EXISTS,INTERSECT = EXISTS,但是 EXCEPT/INTERSECT 的「查询开销」会⽐ NOT EXISTS/EXISTS ⼤很多。
except ⾃动去重复,not in/not exists不会。
2.2 ⽰例
--except
--SQL:
select name from family_member
where group_level > 0
except(select name from family_grade)
--result:
name
rabbit
--NOT EXISTS
--SQL:
select name from family_member
where group_level > 0
and not exists(select name from family_grade where family_member.name = family_grade.name) --result:
name
rabbit
rabbit
三、测试数据
其中验证 except 去重复功能时在 family_member 中新增⼀个 rabbit。
-- ----------------------------
-- Table structure for family_grade
-- ----------------------------
DROP TABLE [mazeytop].[family_grade]
GO
CREATE TABLE [mazeytop].[family_grade] (
[id] int NOT NULL ,
[name] varchar(20) NULL ,
[grade] int NULL
)
GO
-- ----------------------------
-- Records of family_grade
-- ----------------------------
INSERT INTO [mazeytop].[family_grade] ([id], [name], [grade]) VALUES (N'1', N'mazey', N'70') GO
GO
INSERT INTO [mazeytop].[family_grade] ([id], [name], [grade]) VALUES (N'2', N'cherrie', N'93') GO
GO
-- ----------------------------
-- Table structure for family_member
-- ----------------------------
DROP TABLE [mazeytop].[family_member]
GO
CREATE TABLE [mazeytop].[family_member] (
[id] int NOT NULL ,
[name] varchar(20) NULL ,
[sex] varchar(20) NULL ,
[age] int NULL ,
[group_level] int NULL
)
GO
-- ----------------------------
-- Records of family_member
-- ----------------------------
INSERT INTO [mazeytop].[family_member] ([id], [name], [sex], [age], [group_level]) VALUES (N'1', N'mazey', N'male', N'23', N'1')
GO
GO
INSERT INTO [mazeytop].[family_member] ([id], [name], [sex], [age], [group_level]) VALUES (N'2', N'cherrie', N'female', N'22', N'2')
GO
GO
INSERT INTO [mazeytop].[family_member] ([id], [name], [sex], [age], [group_level]) VALUES (N'3', N'rabbit', N'female', N'15', N'3')
GO
GO
INSERT INTO [mazeytop].[family_member] ([id], [name], [sex], [age], [group_level]) VALUES (N'4', N'rabbit', N'female', N'15', N'3')
GO
GO
-- ----------------------------
-- Table structure for family_part
-- ----------------------------
DROP TABLE [mazeytop].[family_part]
GO
CREATE TABLE [mazeytop].[family_part] (
[id] int NOT NULL ,
[group] int NULL ,
[group_name] varchar(20) NULL
)
GO
-- ----------------------------
-- Records of family_part
-- ----------------------------
INSERT INTO [mazeytop].[family_part] ([id], [group], [group_name]) VALUES (N'1', N'1', N'⽗亲')
GO
GO
INSERT INTO [mazeytop].[family_part] ([id], [group], [group_name]) VALUES (N'2', N'2', N'母亲')
GO
GO
INSERT INTO [mazeytop].[family_part] ([id], [group], [group_name]) VALUES (N'3', N'3', N'⼥⼉')
GO
GO
-- ----------------------------
-- Indexes structure for table family_grade
-- ----------------------------
-- ----------------------------
-- Primary Key structure for table family_grade
-- ----------------------------
ALTER TABLE [mazeytop].[family_grade] ADD PRIMARY KEY ([id])
GO
-- ----------------------------
-
- Indexes structure for table family_member
-- ----------------------------
-- ----------------------------
-- Primary Key structure for table family_member
-- ----------------------------
ALTER TABLE [mazeytop].[family_member] ADD PRIMARY KEY ([id])
GO
-- ----------------------------
-- Indexes structure for table family_part
-- ----------------------------
-- ----------------------------
-
- Primary Key structure for table family_part
-- ----------------------------
ALTER TABLE [mazeytop].[family_part] ADD PRIMARY KEY ([id])
GO
版权声明
本博客所有的原创⽂章,作者皆保留版权。转载必须包含本声明,保持本⽂完整,并以超链接形式注明作者和本⽂原始地址:(完)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论