1

我正在尝试使用 SQL Developer 中的 SET 运算符获取计数结果。

我必须找出“table_name1”中有多少“attribute1”但不在“table_name2”中

本质上,我想要从以下查询中获得的结果,但使用 SET 运算符。

SELECT count(distinct <attribute1>)
FROM <table_name1>
WHERE <attribute1> IS NOT (SELECT <attribute1>
                           FROM <table_name2>);

谁能帮帮我吗?

4

3 回答 3

0

请尝试以下解决方案:

SELECT count(distinct <attribute1>)
FROM <table_name1>
WHERE <attribute1> NOT IN (SELECT <attribute1>
                           FROM <table_name2>);

我希望这对你有帮助。

于 2016-10-06T05:49:25.993 回答
0

如果必须使用集合运算符,则可以使用以下方法解决此问题MINUS

SELECT COUNT(*)                      -- use COUNT(DISTINCT attribute1) to avoid
FROM                                 -- duplicates
(
    SELECT attribute1
    FROM table_name1
    MINUS
    SELECT attribute1
    FROM table_name2
) t

但是,我可能会在LEFT JOIN这里使用 a ,因为它在概念上很简单:

SELECT COUNT(DISTINCT t1.attribute1) -- replace with COUNT(*) to count duplicates
FROM table_name1 t1
LEFT JOIN table_name2 t2
    ON t1.attribute1 = t2.attribute1
WHERE t2.attribute1 IS NULL          -- indicates that attribute does NOT appear in
                                     -- the second table
于 2016-10-06T05:49:56.973 回答
0
SELECT COUNT(<attribute1>)
FROM <table_name1>
WHERE <attribute1> MINUS (SELECT <attribute1>
                           FROM <table_name2>);

https://docs.oracle.com/cd/B19306_01/server.102/b14200/operators005.htm

更新的答案

SELECT COUNT(X.id_num)
(SELECT id_num
FROM Tree 
WHERE id_num)
MINUS 
(SELECT id_num 
FROM Bird) AS X
于 2016-10-06T05:50:17.343 回答