简体   繁体   English

Oracle SQL:将所有用户放在一个表中,而不是另一个表中,并加入第三个表

[英]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.

相关问题 Oracle连接到一个表或另一个表 - Oracle join to one table OR another 使用连接查询(oracle sql)时如何查看一个表中的所有数据并在另一个表上过滤 - How to see all data from one table and filtered on another, when using a join query (oracle sql) 从一个表中进行选择,在另一个表上进行联接,在第三个表中进行排序? - SELECT from one table, JOIN on another table, ORDER BY a third table? Oracle SQL 获取第三个表的参考 - Oracle SQL Get Reference of third table SQL:一个表引用另一个表以返回第三个表 - SQL: One table referencing another table to return a third table SQL查询可从一个表中获取所有记录,并在另一个表上进行连接,包括任何其他唯一记录 - SQL Query to get all records from one table and join on another including any additional unique records 通过映射第三个表查询联接两个表,而无需从Oracle中的第三个表返回所有记录 - Query to join two tables by mapping third table without returning all records from third table in Oracle 基于一个表的Oracle sql Join表,该表在另一表的字段中包含单词 - Oracle sql Join tables based on one table containing the word in the field of another table Oracle SQL 将日期维度表与另一个关于日期值的表连接 - Oracle SQL join date dimension table with another table on date value Oracle SQL,试图从选择/联接中获取一个值以用于更新一个表中的一列? - Oracle SQL, trying to get one value from a select/join to use to update one column in one table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM