我需要在三个表之间显示不同的数据。这个要求怎么做。
第一表:
9999999999
8888888888
7777777777
6666666666
5555555555
第二表:
7777777777
9999999999
第三表:
8888888888
我想要这种格式的输出。
6666666666
5555555555
对于您提供给我们的数据,您可以试试这个:
select YourColumn from Table1
minus
select Yourcolumn from Table2
minus
select YourColumn from Table3
但是,这不会为您提供表 3 中存在的条目,但不会为您提供表 1 ND 2 中的条目。我支持您改进问题的建议。
利用LEFT JOIN
SELECT T1."Col"
FROM Table1 T1
LEFT JOIN Table2 T2
ON T1."Col" = T2."Col"
LEFT JOIN Table3 T3
ON T1."Col" = T3."Col"
WHERE T2."Col" IS NULL
AND T3."Col" IS NULL
输出:
| COL |
--------------
| 6666666666 |
| 5555555555 |
您的结果没有显示不同的数据。它显示了不在任何 OTHER 表上的数据
所以你可以这样写
CREATE TABLE FirstTable (VALUE VARCHAR(10))
CREATE TABLE SecondTable (VALUE VARCHAR(10))
CREATE TABLE ThirdTable (VALUE VARCHAR(10))
INSERT INTO FirstTable VALUES(9999999999)
INSERT INTO FirstTable VALUES(8888888888)
INSERT INTO FirstTable VALUES(7777777777)
INSERT INTO FirstTable VALUES(6666666666)
INSERT INTO FirstTable VALUES(5555555555)
INSERT INTO SecondTable VALUES(9999999999)
INSERT INTO SecondTable VALUES(7777777777)
INSERT INTO ThirdTable VALUES(8888888888)
SELECT a1.vALUE FROM (SELECT a.vALUE FROM FirstTable a
LEFT OUTER JOIN SecondTable b ON A.VALUE=B.VALUE
WHERE b.VALUE IS NULL) a1
LEFT OUTER JOIN ThirdTable c ON A1.VALUE=C.VALUE
WHERE c.VALUE IS NULL
与流行的倾向相反,我认为这是一个好问题!
以下解决方案all unique values
在三个表中找到:
with middle_tab as(
select t1.id t1_id,t2.id t2_id,t3.id t3_id from tab1 t1
full outer join tab2 t2 on t1.id=t2.id
full outer join tab3 t3 on t1.id=t3.id
)
select coalesce(t1_id,t2_id,t3_id) unique_ids from (
select t1_id,t2_id,t3_id,NVL2(t1_id,0,1)+NVL2(t2_id,0,1)+NVL2(t3_id,0,1) b
from middle_tab)
where b>1
尝试这个...
Select yourColumn
from Table1
where yourColumn not in
( select yourColumn from Table2)
and yourColumn not in
(select yourColumn from table3)
尝试:
SELECT Column FROM FirstTable
WHERE Column NOT IN
(SELECT Column from SecondTable UNION
SELECT Column from ThirdTable)