where exists的使用(SQL中EXISTS怎么用)

本文目录
- SQL中EXISTS怎么用
- 关于sql中where exists的问题
- 怎么在where子句中用case when + exists
- exists子查询结果的使用
- SQL中 exists是怎样应用的
- oracle中exists用法
SQL中EXISTS怎么用
EXISTS在SQL中的作用是:检验查询是否返回数据。
select a.* from tb a where exists(select 1 from tb where name =a.name)返回真假,当 where 后面的条件成立,则列出数据,否则为空。
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 方法描述如果在 Dictionary对象中指定的关键字存在,返回True,若不存在,返回False。
语法object.Exists(key)Exists 方法语法有如下几部分:部分描述Object必需的。始终是一个 Dictionary对象的名字。Key必需的,在 Dictionary对象中搜索的Key值。
exist相当于存在量词:表示集合存在,也就是集合不为空只作用一个集合.
例如 exist P表示P不空时为真; not exist P表示p为空时为真in表示一个标量和一元关系的关系。
例如:s in P表示当s与P中的某个值相等时为真; s not in P 表示s与P中的每一个值都不相等时为真。
exits与not exits
exists(sql 返回结果集为真)
not exists(sql 不返回结果集为真)
如果not exists子查询只有自己本身的查询条件,这样只要子查询中有数据返回,就证明是false,结果在整体执行就无返回值;一旦跟外面的查询关联上,就能准确查出数据。
参考资料:百度百科-Exists
关于sql中where exists的问题
EXISTS
指定一个子查询,检测行的存在。
语法
EXISTS subquery
参数
subquery
是一个受限的 SELECT 语句 (不允许有 COMPUTE 子句和 INTO 关键字)。有关更多信息,请参见 SELECT 中有关子查询的讨论。
结果类型
Boolean
结果值
如果子查询包含行,则返回 TRUE。
怎么在where子句中用case when + exists
简单的用法比如:
selelct user_id, user_name
from tb_users
where user_id in (select user_id from tb_course)
这个查询是查询在tb_course表中存在的user记录。
使用exists,写成
select user_id, user_name
from tb_users
where exists (select 1 from tb_course where tb_course.user_id = tb_users.user_id)
exists用法是把主查询中的字段传入到子查询中去。如果有符合的条件,会停止全表检索,返回TRUE。所以效率才要高于IN,IN是要进行完全表检索得到集合才会结束执行。而EXISTS遇到符合的 条件,就会停止执行子查询。
exists子查询结果的使用
不需要使用exists,直接使用一个子查询就可以。能满足你的要求,可以在查询中使用子查询中结果。
SELECT b.Id,
Totalproduct / (b.Usetime)
FROM Detail a,
(SELECT NAME,
Id,
Datediff(Getdate(), Creatdate) AS Usetime
FROM Product) b
WHERE b.Id = a.Id
SQL中 exists是怎样应用的
exists其实和in类似的用法
SELECT id, category_id, htmlfile, title, convert(varchar(20),begintime,112) as pubtime
FROM tab_oa_pub WHERE is_check=1 and
category_id in (select id from tab_oa_pub_cate where no=’1’)
order by begintime desc
改成exists则为
SELECT id, category_id, htmlfile, title, convert(varchar(20),begintime,112) as pubtime
FROM tab_oa_pub WHERE is_check=1 and
exists (select id from tab_oa_pub_cate where tab_oa_pub.category_id=convert(int,no) and no=’1’)
order by begintime desc
oracle中exists用法
exists可以说是oracle数据库开发中比较常见的用法,用exists可以提高sql的效率,可以取代in。
比如 a,b 关联列为 a.id = b.id,现在要取 a 中的数据,其中id在b中也存在:
select * from a where exists(select 1 from b where a.id = b.id)现在要取 a 中的数据,其中id在b中 不存在:
select * from a where not exists(select 1 from b where a.id = b.id)
用法详解
exists是判断exits后面的sql语句是否为真,若为真则整个sql句子成立,否则没有任何记录。例子:
select 1 from dual where exists (select 1 from dual where 2=1);
上面的情况肯定是没有记录。
select 1 from dual where exists (select 1 from dual where 1=1);
上面的情况是有记录返回的。
判断另外的表中是否包含某个表的外键记录。例子:
select * from table_test a
where exists (select 1 from scott.carol_tmp where pps_master=a.pps_master);
这个sql是要检查table_test中的pps_master是否在carol_tmp中。其实用in也可以实现同样的效果,但是in的话效率要低些,特别是碰上一些大表。用exists和in的性能就体现出来了。

更多文章:
易语言网页api接口怎么调用(易语言,怎么读取网页json的api)
2026年10月11日 08:00
majority of(the majority of 和 a majority of的区别以及用法例句)
2026年10月11日 07:40
another time(another time和other time的区别)
2026年10月11日 05:00








