在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、exists1.1 说明EXISTS(包括 NOT EXISTS)子句的返回值是一个 BOOL 值。EXISTS 内部有一个子查询语句(SELECT ... FROM...),我将其称为 EXIST 的内查询语句。其内查询语句返回一个结果集。 EXISTS 子句根据其内查询语句的结果集空或者非空,返回一个布尔值。 exists:强调的是是否返回结果集,不要求知道返回什么,比如: 相对于 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: name 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-21intersect 的作用与 exists 类似。 --intersect --SQL: select name from family_member where group_level > 0 intersect select name from family_grade where grade > 90 --result: name cherrie 二、except2.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 到此这篇关于SQLServer中exists和except用法介绍的文章就介绍到这了,更多相关SQLServer中exists和except内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论