【发布时间】:2017-09-05 05:46:40
【问题描述】:
我需要构建一个查询,以便始终显示表 A 中的记录,以及当 2 列匹配时显示表 B 中的记录。
表 A 包含一个记录列表,我想一直显示,即 Slot List 1 - 3。
表 B 有一个动态值列表,其中 1 列 (Table_B_Location_Code) 已经分配了与表 A 匹配的标识符,但第二列 (Table_B_Slot_ID ) 稍后会更新。
最好的情况是,表 A 内容一直显示,因此表 B 内容具有 Table_B_Location_Code 和 Table_B_Slot_ID 分配,让它出现。
另外,我在表 B 中有一个日期列。这意味着,如果日期等于 sysdate (to_char(TABLE_B_DATE,'ddmmyyyy') = to_char(sysdate,'ddmmyyyy')),我只想考虑表 B 中的项目。
我猜这是一个左外连接。但我无法让它工作。
CREATE TABLE Table_A (
Table_A_ID int,
Table_A_Location_Code varchar(255),
Table_A_Slot_ID int
);
CREATE TABLE Table_B (
Table_B_ID int,
Table_B_Location_Code varchar(255),
Table_B_Slot_ID int,
TABLE_B_DATE date);
INSERT INTO Table_A (Table_A_ID,Table_A_Location_Code,Table_A_Slot_ID)
VALUES (1, 'Room1', 1);
INSERT INTO Table_A (Table_A_ID,Table_A_Location_Code,Table_A_Slot_ID)
VALUES (1, 'Room1', 2);
INSERT INTO Table_A (Table_A_ID,Table_A_Location_Code,Table_A_Slot_ID)
VALUES (1, 'Room1', 3);
INSERT INTO Table_B (Table_B_ID,Table_B_Location_Code,Table_B_Slot_ID,TABLE_B_DATE)
VALUES (1, 'Room1', 1, 12-04-2017);
INSERT INTO Table_B (Table_B_ID,Table_B_Location_Code,Table_B_Slot_ID,TABLE_B_DATE)
VALUES (1, 'Room1', 0, 12-01-2017);
INSERT INTO Table_B (Table_B_ID,Table_B_Location_Code,Table_B_Slot_ID,TABLE_B_DATE)
VALUES (1, 'Room1', 0, 12-04-2017);
Select * from Table_A;
Select * from Table_B;
【问题讨论】:
标签: sql oracle full-outer-join