SQL 两表关联查询 查询出A表中有但是B表中没有的数据

19073人阅读
Database SqlServer(5)
(转)A、B两表,找出ID字段中,存在A表,但是不存在B表的数据。A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引。
  使用 not in ,容易理解,效率低& ~执行时间为:1.395秒~
1 select distinct A.ID from
A where A.ID not in (select ID from B)
  使用 left join...on... , &B.ID&isnull&
表示左连接之后在B.ID 字段为 null的记录& ~执行时间:0.739秒~
1 select A.ID from A left join B on A.ID=B.ID where B.ID is null
  逻辑相对复杂,但是速度最快& ~执行时间: 0.570秒~
select * from
where (select count(1) as num from A where A.ID = B.ID) = 0
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:182999次
积分:1871
积分:1871
排名:千里之外
原创:35篇
转载:30篇
评论:15条
(1)(1)(1)(2)(5)(1)(2)(2)(2)(5)(5)(2)(11)(2)(1)(2)(2)(1)(4)(3)(2)(1)(7)今天看啥 热点:
SQL语句技巧:查询存在一个表而不在另一个表中的数据记录,sql语句方法一(仅适用单个字段)使用 not in ,容易理解,效率低select A.ID from A where A.ID not in (select ID from B)方法二(适用多个字段匹配)使用 left join...on... , "B.ID isnull" 表示左连接之后在B.ID 字段为 null的记录select A.ID from A left join B on A.ID=B.ID where B.ID is null&
方法三(适用多个字段匹配)
select * from B where (select count(1) as num from A where A.ID = B.ID) = 0
方法四(适用多个字段匹配)
select * from A where not exists(select 1 from B where A.ID=B.ID)
同步发表于我的个人网站:/post//54.html
select top 1 *from tb2where id not in (select [index] from tb1)order by id
select * from table1 where id not in (select id from table2)
相关搜索:
相关阅读:
相关频道:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&
Sql Server最近更新查看:4060|回复:8
A表& && && && &&&B表
-------& && && & -------
1& && && && && &&&1
2& && && && && &&&2
3& && && && && &&&4
A表的3该用什么语句查询出来?
虽然是基础的问题,但值得大家一议 ...
中级工程师
select * from a where not exists(select * from b where a.col=col)
select col&&from a
select col&&from b
:handshake
select * from a where not exists(select 1 from b where a.col=col)
中级工程师
学习。。。。。。
来学习学习!
:victory: 学习了

我要回帖

更多关于 jpa多表关联查询 的文章

 

随机推荐