我有一个表Table1
和Table2
,如下所示:
表1
Col1
====
A
B
表2
Col1
====
A
B
C
D
现在使用INNER JOIN
我需要找到Table2
(注意:WHERE
子句不允许)
预期输出:
Col1
====
C
D
SQL查询可以是什么?
我已经尝试过它不能正常工作了。
Select Distinct
Table2.col1
from
Table1
Inner Join
Table2 On Table1.col1 <> Table2.col1
答案 0 :(得分:0)
第一次跳枪 - 应该读得不错,对不起。不得不考虑这个:
SELECT Table2.col1
FROM Table1 RIGHT JOIN Table2
ON Table1.col1 = Table2.col1
GROUP BY Table1.col1, Table2.col1
HAVING Table1.col1 IS NULL;
答案 1 :(得分:0)
您可以使用以下查询
SELECT * FROM Table2
WHERE Col1 NOT IN
(SELECT Col1 FROM Table1)
以下查询也适用
SELECT Col1 FROM Table2
EXCEPT
SELECT Col1 FROM Table1
答案 2 :(得分:0)
with
t1 as (
select col1 as c1
, row_number() over (order by col1 ASC) as rn1
from table1
),
t2 as (
select col1 as c2
, row_number() over (order by col1 DESC) as rn2
from table2
)
select
c2 as Col1
from t2
inner join t1 on t2.rn2 = t1.rn1 and t2.c2 <> t1.c1
order by c2 ASC
;
http://sqlfiddle.com/#!15/4747e/1(postgres 9.3.1)