SQL EXISTS 与 IN

EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False
EXISTS 指定一个子查询,检测行的存在。

EXISTS与IN的使用效率的问题,通常情况下采用exists要比in效率高,因为IN不走索引,但要看实际情况具体使用:
IN适合于外表大而内表小的情况;EXISTS适合于外表小而内表大的情况。

实例:(EXISTS)

select * from dbo.PurchaseSettleAccountsDetails d where exists(select * from dbo.PurchaseSettleAccounts where ApprovalStatus=1 and ID=d.PurchaseSettleAccountId)

(IN)

select * from dbo.PurchaseSettleAccountsDetails d where PurchaseSettleAccountId IN (select ID from dbo.PurchaseSettleAccounts where ApprovalStatus=1 and ID=d.PurchaseSettleAccountId)

时间: 2024-08-27 08:50:05

SQL EXISTS 与 IN的相关文章

sql Exists与in 的区别

由于要使用 in,后来在网上找找了,发现有如下文章,大家共享一下.一起进步啦. 使用in的话,如果存在索引,和使用 join 性能差别不大. 转载自http://blog.chinaunix.net/u/4929/showart_1075412.html IN 确定给定的值是否与子查询或列表中的值相匹配. EXISTS 指定一个子查询,检测行的存在. 比较使用 EXISTS 和 IN 的查询 这个例子比较了两个语义类似的查询.第一个查询使用 EXISTS 而第二个查询使用 IN.注意两个查询返回

LINQ系列:LINQ to SQL Exists/In/Any/All/Contains

1. Any 返回没有Product的Category var expr = from c in context.Categories where !c.Products.Any() select c; SELECT [Extent1].[CategoryID] AS [CategoryID], [Extent1].[CategoryName] AS [CategoryName] FROM [dbo].[Category] AS [Extent1] WHERE NOT EXISTS (SELEC

SQL exists( select 1 from

use UnlockIndustry select * from Info_Coordinate as A join Info_Employee on A.EmployeeId=Info_Employee.EmployeeId Where exists( select 1 from ( select EmployeeId,MAX(CreateTime) as CreateTime from Info_Coordinate group by Info_Coordinate.EmployeeId )

SQL Exists 的用法 转载

比如在Northwind数据库中     有一个查询为 SELECT c.CustomerId, CompanyName FROM Customers c WHERE EXISTS( SELECT OrderID FROM Orders o WHERE o.CustomerID = cu.CustomerID) 这里面的EXISTS是如何运作呢?子查询返回的是OrderId字段,可是外面的查询要找的是CustomerID和CompanyName字段,这两个字段肯定不在OrderID里面啊,这是如

数据库——SQL中EXISTS怎么用2(转)

数据库sql语句的exists总结 sql exists in 学习 先来比较下语法: --deals=交易表,areas=地域表,例如香港:我们的目的:查看有交易的地域 select * from areas where id in (select city_id from deals); select * from areas where id in   (select city_id from deals where deals.city_id = areas.id); select *

sql中exists和notexists用法总结(并和in的比较)

首先头脑中有三点概念: 1.EXISTS子查询找到的提交 NOT EXISTS 子查询中 找不到的提交 说明:不要去翻译为存在和不存在,把脑袋搞晕. 2.建立程序循环的概念,这是一个动态的查询过程.如 FOR循环 . 3.Exists执行的流程Exists首先执行外层查询,再执行内存查询,与IN相反. 流程为首先取出外 层中的第 一 元组, 再执行内层查询,将外层表的第一元组代入,若内层查询为真,即有结果 时.返回外层表中的第一元组,接着取出第二元组,执行相同的算法.一直到扫描完外层整表 . ?

SQL语句教程

SQL指令 SELECT DISTINCT WHERE AND OR IN BETWEEN LIKE ORDER BY 函数 COUNT GROUP BY HAVING ALIAS 表格链接 外部链接 CONCATENATE SUBSTRING TRIM 表格处理 CREATE TABLE CONSTRAINT NOT NULL UNIQUE CHECK 主键 外来键 CREATE VIEW CREATE INDEX ALTER TABLE DROP TABLE TRUNCATE TABLE I

小表驱动大表, 兼论exists和in

给出两个表,A和B,A和B表的数据量, 当A小于B时,用exists select * from A where exists (select * from B where A.id=B.id) exists的实现,相当于外表循环,每次循环对内表进行查询? for i in A for j in B if j.id == i.id then .... 相反,如果A大于B的时候,则用in select * from A where id in (select id from B) 这种在逻辑上类似

Stream Processing 101: From SQL to Streaming SQL in 10 Minutes

原文:https://wso2.com/library/articles/2018/02/stream-processing-101-from-sql-to-streaming-sql-in-ten-minutes/ We have entered an era where competitive advantage comes from analyzing, understanding, and responding to an organization’s data. When doing