0

使用 oracle developer,我运行了一个查询,结果如下表。但我只想要 column1 与所有值列 2 (3,4,8) 匹配的结果。所以输出将是 2、3,但不是 4。我确信有一种方法可以在不进行硬编码的情况下实现这个结果?我在想它的某种自我加入?

    select column1, column2
    from table1
    where column1 in (
        select column1
        from table2
        where depth >= 100)
    order by column2;

输出:

    column1   column2
    3          2
    8          2
    4          2
    3          3
    4          3
    8          3
    4          4

表2

    Column1     Area_Name       Depth
    1           Lake            40
    2           River           50
    3           Ocean           150
    4           Cliff           150
    5           Mountain        90
    6           Construction    60
    7           Building        50
    8           Random          100
    9           Also Random     50
    10          Another one     80

需要的输出:

    column2
    2
    3

好的,这就是我要找的:

    SELECT table1.column1
    FROM table1
        INNER JOIN table2 
        ON table1.column2 = table2.column2
        WHERE table2.depth >= 100
    GROUP BY boat_id
    HAVING COUNT(*) >= (
        select count(*) 
        from table2
        where depth >= 100); 
4

1 回答 1

0

更新

WITH qry AS (
  SELECT column1, column2
    FROM table1
   WHERE column1 IN (
        SELECT column1
          FROM table2
         WHERE depth >= 100)
) 
SELECT t1.column2
  FROM qry t1 LEFT JOIN qry t2
    ON t1.column1 = t2.column1 AND t1.column2 = t2.column2
 GROUP BY t1.column2
HAVING COUNT(*) = (SELECT COUNT(DISTINCT column1) FROM qry)
ORDER BY t1.column2

输出:

| COLUMN2 |
-----------
|       2 |
|       3 |

SQLFiddle

于 2013-05-19T20:41:33.607 回答