[英]Oracle SQL: Get all users in one table but not another and join to a third table
I am wondering how to use oracle sql to get all the rows that are in one table but not another. 我想知道如何使用oracle sql来获取一个表而不是另一个表中的所有行。 The issue I am having is that the two tables don't have a field in common so I need to join to a third master table. 我遇到的问题是两个表没有相同的字段,因此我需要加入第三个主表。
This is what I've tried which doesn't produce any errors but also produces 0 records which isn't possible but clearly I've done something wrong. 这是我尝试过的操作,不会产生任何错误,但是会产生0条记录,这不可能,但显然我做错了。
SELECT a.USER_ID, c.AD_ID, c.CREATED_DATE_ FROM $A$ a, $C$ c, $B$ b
WHERE (b.USER_ID IS NULL AND a.CUSTOMER_ID = c.CUSTOMER_ID)
I have three tables: 我有三个表:
Table A has fields CUSTOMER_ID & USER_ID 表A具有字段CUSTOMER_ID和USER_ID
Table B has field USER_ID 表B具有字段USER_ID
Table C has field CUSTOMER_ID 表C具有字段CUSTOMER_ID
I need all the users that are in table C but not table B. They are all in Table A because that is the master list of users. 我需要表C中的所有用户,而不是表B中的所有用户。它们都在表A中,因为这是用户的主列表。
Any insight would be greatly appreciated. 任何见识将不胜感激。
SELECT
*
FROM
table_a
WHERE
NOT EXISTS (SELECT * FROM table_b WHERE table_b.user_id = table_a.user_id )
AND EXISTS (SELECT * FROM table_c WHERE table_c.customer_id = table_a.customer_id)
My solution: 我的解决方案:
select * from TableC tc
join TableA ta on tc.CUSTOMER_ID=ta.CUSTOMER_ID
left join TableB tb on tb.USER_ID=ta.USER_ID
where ta.USER_ID is null
I think you want: 我想你要:
select a.USER_ID, c.AD_ID, c.CREATED_DATE_
from a join
c
on a.customer_id = c.customer_id
where not exists (select 1 from b where b.user_id = a.user_id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.