1.2 示例EXISTS(包括 NOT EXISTS)子句的返回值是一个BOol值。EXISTS内部有一个子查询语句(SELECT ... FROM...),我将其称为EXIST的内查询语句。其内查询语句返回一个结果集。EXISTS子句根据其内查询语句的结果集空或者非空,返回一个布尔值。Link
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效率要差点,应该是不会执行索引的原因。Link
相对于inner join,exists性能要好一些,当她找到第一个符合条件的记录时,就会立即停止搜索返回TRUE。
--EXISTS--sql:select name from family_memberwhere group_level > 0and exists(select 1 from family_grade where family_member.name = family_grade.nameand grade > 90)--result:namecherrIE--NOT EXISTS--sql:select name from family_memberwhere group_level > 0and not exists(select 1 from family_grade where family_member.name = family_grade.nameand grade > 90)--result:namemazeyrabbit二、except 2.1 说明
2.2 示例查询结果上EXCEPT = NOT EXISTS,INTERSECT = EXISTS,但是EXCEPT / INTERSECT的「查询开销」会比NOT EXISTS / EXISTS大很多。
except自动去重复,not in / not exists不会。
--except--sql:select name from family_memberwhere group_level > 0except(select name from family_grade)--result:namerabbit--NOT EXISTS--sql:select name from family_memberwhere group_level > 0and not exists(select name from family_grade where family_member.name = family_grade.name)--result:namerabbitrabbit三、测试数据
-- ------------------------------ table structure for family_grade-- ----------------------------DROP table [mazeytop].[family_grade]GOCREATE 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')GOGOINSERT INTO [mazeytop].[family_grade] ([ID], [grade]) VALUES (N'2', N'cherrIE', N'93')GOGO-- ------------------------------ table structure for family_member-- ----------------------------DROP table [mazeytop].[family_member]GOCREATE table [mazeytop].[family_member] ([ID] int NOT NulL ,[sex] varchar(20) NulL ,[age] int NulL ,[group_level] int NulL )GO-- ------------------------------ Records of family_member-- ----------------------------INSERT INTO [mazeytop].[family_member] ([ID], [sex], [age], [group_level]) VALUES (N'1', N'male', N'23', N'1')GOGOINSERT INTO [mazeytop].[family_member] ([ID], [group_level]) VALUES (N'2', N'female', N'22', N'2')GOGOINSERT INTO [mazeytop].[family_member] ([ID], [group_level]) VALUES (N'3', N'rabbit', N'15', N'3')GOGOINSERT INTO [mazeytop].[family_member] ([ID], [group_level]) VALUES (N'4', N'3')GOGO-- ------------------------------ table structure for family_part-- ----------------------------DROP table [mazeytop].[family_part]GOCREATE 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'父亲')GOGOINSERT INTO [mazeytop].[family_part] ([ID], [group_name]) VALUES (N'2', N'2', N'母亲')GOGOINSERT INTO [mazeytop].[family_part] ([ID], [group_name]) VALUES (N'3', N'3', N'女儿')GOGO-- ------------------------------ 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用法全部内容,希望文章能够帮你解决SQLServer中exists和except用法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)