mysql not exists查找A表某列的数据不存在B表

 people表的某些id在org表里面找不到,需要找出来是people表的哪些org_id

1、not exists写法
SELECT * FROM people t1
 WHERE NOT EXISTS (SELECT 1 FROM org t2 WHERE t1.org_id = t2.org_id)

2、not in写法
SELECT * FROM tq_data0904.tb_pp_community_correction_people t1
 WHERE org_id NOT IN (SELECT org_id FROM hczz.tq_org t2 )

3、带条件
SELECT * FROM 
(SELECT * FROM hczz.tq_org WHERE org_type = "community") t1
WHERE t1.org_id NOT IN
 (SELECT t1.parent_community_id AS org_id FROM hczz.tq_org t1
	LEFT JOIN tb_pp_community_correction_people t2 ON t1.org_id = t2.org_id 
	GROUP BY t1.parent_community_id ) 





 

你可能感兴趣的:(Mysql数据库)