A、B两表使用ID关联,找出ID存在A表,但是不存在B表的数据。

select distinct A.ID from A where A.ID not in (select ID from B)

A、B两表使用ID关联,删除ID存在A表,但是不存在B表的数据。

delete
from A where A.ID not in (select ID from B)

https://www.cnblogs.com/zqifa/p/sql-select-1.html

1.select distinct A.ID from A where A.ID not in (select ID from B) 2.select A.ID from A left join B on A.ID=B.ID where B.ID is null 3.select * from B where (select count(1) as num from A where A.ID = B.ID) = 0 查询少量数据. select distinct 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 ... 在做项目的时候遇到一个问题:我需要查询A表存在而B表不存在数据。第一想法便是使用not in,当时的sql是这样的。接着在网上查了一下关于not in语法,想具体了解的话大家可以看一下这位大佬的文章。看着简单,但查询的结果为空,瞬间头大,第一想法就是What happened?大致意思就是数据中有null值,查询不到数据,同时大佬也给了解决的方案。另外分享一下我自己的解决方法。 select * from ti_road_node_code a where a.road_node_id not in ( select en_road_node_id from ti_road_node_relation b) select * from ti_road_n... 案例2:关于如何优化表A不存在于表B的语句 在正常这种情况的时候,大部分人的第一感觉就是使用SqlServer的not in,select [列名] from table1 where [列名] not in (select [列名] from table2), 这种办法的效率低下着实让编程人员甚是头疼,介于这种原因,我们可以考虑使用SqlServer自带的except来代替这种写法。 本系统为@牛旦教育IT课堂在微头条上的内容,为便于查阅,特辑录于此,都是常用SQL基本用法。。前两篇连接:(一):SQL点滴(查询篇):数据库基础查询案例实战(二):SQL点滴(排序篇):数据常规排序查询实战示例(17)如何实现多表记录的叠加返回?也就是如何将来自多个表的数据组织到一起,把各个表的结果叠加起来。这些表不必要有相同的关键字,但各表对应的列的数据类型应相同。例如我们现实部门编号为2的所... A、B两表,找出ID字段中,存在A表,但是不存在B表的数据。 方法一:使用 not inselect distinct 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... 假设有A、B两张表。如果查询在A表中存在,但是在B表中存在的记录,应该如何操作?1、示例数据假设A表数据:B表数据:其中,B表中的a_id是需要排除的A表的id。这里我们要排除A表中id为3的记录,实际中可能有上万条记录。2、子查询方法一般我们首先想到的可能就是not in语句:selectA.*fromAwhereA.idnotin(selectB.a_idfromB);这... 假设有A、B两张表。如果查询在A表中存在,但是在B表中存在的记录,应该如何操作?示例数据假设A表数据:id12345B表数据:ida_id13其中,B表中的a_id是需要排除的A表的id。这里我们要排除A表中id为3的记录,实际中可能有上万条记录。子查询方法一般我们首先想到的可能就是not in语句:select A.* from A where A.id not in(select B.a_i... 使用left join … on … select * from A left join B on A.dp_account = B.dp_account where B.dp_account is null; 使用not in select distinct A.dp_account from A where A.dp_account not in (select dp_account from B) 摘要:日常开发中经常遇到这样的问题,某一个id在A表中,但是不存在在B表中,那么我们如何查询有多少存在A表,不存在B表的数据呢,我想大部分人首先想到的肯定是not in语法,这里我分享几种除了not in之外的写法,并且效率也比not in高。一:业务场景有这样的两个表:用户表,会员表,其中会员表里面有用户ID标识,正常的业务场景是,用户激活的时候创建对应的会员,这样就能把会员和用户关联起来,其中... 子查询统计的是关联上的记录,等于0就是关联不上,就不存在(是方法三的三倍) select * from a where (select count(1) as num from b where b.id = a.id) = 0 select distinct a.id from a where a.id not in (select id from b); left join…o sb = new StringBuffer("SELECT a.* "); sb.append(" FROM Table_A a "); sb.append(" LEFT JOIN B b ON a.CLJCRLSH = b.CLJCRLSH and a.jccs =b.jccs "); sb.append(" where "); 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…...