SQL查询左外连接到当前查询

时间:2018-04-27 05:20:39

标签: sql oracle

我有一个查询,我想查找其他表中的值作为参考,但不会弄乱我当前的查询结果。我想我必须使用外部左连接,但不知道如何将其与我当前的查询合并。

我当前的查询与此类似:

SELECT a.primary_key,
       a.phase,
       b.project_number,
       c.LENGTH,
       d.color
  FROM TableA a,
       TableB b,
       TableC c,
       TableD d
 WHERE     c.primary_key = a.PROJECT_ID
       AND b.primary_key = a.PROJECT_ID
       AND b.primary_key = d.project_ID
       AND (c.date IS NULL OR c.number IS NULL)
       AND d.color IN ('black','red','blue')
ORDER BY 1

现在,这给了我一张包含50个结果的表格。 ' TableContacts'具有我的b.project_number的查找值。所以说我的50个结果表,其中只有10个有b.project_number,我需要来自' TableContacts'的查找值。也显示在我的结果中,但我不希望这会影响我的结果并将其降低到10,我仍然需要我原来的50个结果,只需要附加信息。帮助

2 个答案:

答案 0 :(得分:1)

只需将CONTACTS表添加到您的联接中:

SELECT a.primary_key,
       a.phase,
       b.project_number,
       c.LENGTH,
       d.color, 
       ct.lookup_value --<< this is from the CONTACTS table
FROM TableA a
  JOIN TableB b ON b.primary_key = a.PROJECT_ID
  JOIN TableC c ON c.primary_key = a.PROJECT_ID
  JOIN TableD d ON b.primary_key = d.project_ID
  LEFT JOIN contacts ct ON ct.some_column = b.project_Number --<< this is the outer join to the CONTACTS table
WHERE (c.date IS NULL OR c.number IS NULL)
  AND d.color IN ('black','red','blue')
ORDER BY 1

当您对表名和列名进行模糊处理时,很难猜测CONTACTS表上的连接条件应该是多么接近。

答案 1 :(得分:0)

你可以使用这种方法:

创建一个包含当前查询结果的表格:

create table t1 as 
SELECT a.primary_key,
       a.phase,
       b.project_number,
       c.LENGTH_col,
       d.color
FROM a,b,c,d
WHERE c.primary_key = a.PROJECT_ID
       AND b.primary_key = a.PROJECT_ID
       AND b.primary_key = d.project_ID
       AND (c.date_col IS NULL OR c.number_col IS NULL)
       AND d.color IN ('black','red','blue')
ORDER BY 1;

使用联合来获得所需的结果:

select t1.primary_key, t1.phase, t1.project_number, t1.length_col, t1.color, TableContacts.lookup_column
from t1, TableContacts
where t1.project_number = TableContacts.project_number
UNION
select t1.primary_key, t1.phase, t1.project_number, t1.length_col, t1.color, null
from t1 where t1.project_number is null;

通过创建虚拟数据进行说明:

select * from a;
PRIMARY_KEY | PROJECT_ID | PHASE
1   100 Phase-1
2   200 Phase-2
3   300 Phase-3
4   400 Phase-4
5   500 Phase-5

select * from b;
PRIMARY_KEY | PROJECT_NUMBER
100 null
200 2000
300 3000
400 null
500 5000

select * from c;
PRIMARY_KEY | NUMBER_COL | LENGTH_COL | DATE_COL
100 null    99  null
200 null    99  null
300 null    99  null
400 null    99  null
500 null    99  null

select * from d;
PROJECT_ID  | COLOR
100 black
200 red
300 blue
400 black
500 yellow

select * from TableContacts;
PROJECT_NUMBER  | LOOKUP_COLUMN
1000    l-1000
2000    l-2000
3000    l-3000
4000    l-4000
5000    l-5000

有问题的现有查询会返回:

SELECT a.primary_key,
       a.phase,
       b.project_number,
       c.LENGTH_col,
       d.color
FROM a,b,c,d
WHERE c.primary_key = a.PROJECT_ID
       AND b.primary_key = a.PROJECT_ID
       AND b.primary_key = d.project_ID
       AND (c.date_col IS NULL OR c.number_col IS NULL)
       AND d.color IN ('black','red','blue')
ORDER BY 1;

PRIMARY_KEY | PHASE | PROJECT_NUMBER | LENGTH_COL | COLOR
1   Phase-1 null    99  black
2   Phase-2 2000    99  red
3   Phase-3 3000    99  blue
4   Phase-4 null    99  black

目标是填充lookup_column project_number不为空的地方。运行在答案开头提供的联合查询:

select t1.primary_key, t1.phase, t1.project_number, t1.length_col, t1.color, TableContacts.lookup_column
from t1, TableContacts
where t1.project_number = TableContacts.project_number
UNION
select t1.primary_key, t1.phase, t1.project_number, t1.length_col, t1.color, null
from t1 where t1.project_number is null;

PRIMARY_KEY | PHASE | PROJECT_NUMBER | LENGTH_COL | COLOR | LOOKUP_COLUMN
1   Phase-1 null    99  black   null
2   Phase-2 2000    99  red     l-2000
3   Phase-3 3000    99  blue    l-3000
4   Phase-4 null    99  black   null