根据表优先级提取id

mfuanj7w  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(251)
CREATE TABLE T( ID  VARCHAR2(100), DUEDAY VARCHAR(400))
CREATE TABLE T1( ID  VARCHAR2(100), DUEDAY VARCHAR(400))

INSERT INTO T VALUES ('500001', '13')
INSERT INTO T VALUES ('500002', '13')
INSERT INTO T VALUES ('500003', '13')
INSERT INTO T1 VALUES ('500001', '14')
SELECT T.* , 'T TABLE' FROM T

UNION ALL

SELECT T1.*, 'T1 TABLE' FROM T1

如您所见,ID500001的数据显示在表t1中。如果dueday是14,那么即使它出现在表t中,也不应该在13提取。

预计产量:500002 500003

r7knjye2

r7knjye21#

试试这个。

SELECT T1.* FROM T1
                WHERE T1.DUEDAY = 13
UNION ALL
SELECT T.* FROM T 
               LEFT JOIN T1 ON t1.ID = t.ID
               WHERE T.DUEDAY = 13 AND T1.ID IS NULL

相关问题