如何从具有ID的表中选择行,而这些ID在PostgreSQL的内部联接中不存在?

时间:2019-09-29 00:14:51

标签: sql postgresql inner-join

我有下表

postgres=# select * from joins_example;
 user_id | price  | id |          email           
---------+--------+----+--------------------------
       1 | $30.00 |    | 
       5 | $50.00 |    | 
       7 | $20.00 |    | 
         |        |  1 | hadil@example.com
         |        |  5 | saiid@example.com
         |        |  2 | fahir@example.com
       6 | $60.00 |  6 | oma@example.com
       8 | $40.00 |  8 | nasim@example.com
         |        |  8 | nasim.hassan@example.com
       9 | $40.00 |  9 | farah@example.com
       9 | $70.00 |    | 
      10 | $80.00 |    | majid@example.com
         |        | 10 | majid.seif@example.com
(13 rows)

user_idid之间的自我内部联接产生

postgres=# select * from joins_example as x inner join joins_example as y on x.user_id = y.id;
 user_id | price  | id |       email       | user_id | price  | id |          email           
---------+--------+----+-------------------+---------+--------+----+--------------------------
       1 | $30.00 |    |                   |         |        |  1 | hadil@example.com
       5 | $50.00 |    |                   |         |        |  5 | saiid@example.com
       6 | $60.00 |  6 | oma@example.com   |       6 | $60.00 |  6 | oma@example.com
       8 | $40.00 |  8 | nasim@example.com |         |        |  8 | nasim.hassan@example.com
       8 | $40.00 |  8 | nasim@example.com |       8 | $40.00 |  8 | nasim@example.com
       9 | $40.00 |  9 | farah@example.com |       9 | $40.00 |  9 | farah@example.com
       9 | $70.00 |    |                   |       9 | $40.00 |  9 | farah@example.com
      10 | $80.00 |    | majid@example.com |         |        | 10 | majid.seif@example.com
(8 rows)

我想要的是:

 user_id | price  | id |       email       | user_id | price  | id |          email           
---------+--------+----+-------------------+---------+--------+----+--------------------------
       7 | $50.00 |    |                   |         |        |    |
         |        |    |                   |         |        |  2 | fahir@example.com

或:

 user_id | price  | id |       email       | user_id | price  | id |          email           
---------+--------+----+-------------------+---------+--------+----+--------------------------
         |        |    |                   |       7 | $50.00 |    |
         |        |  2 | fahir@example.com |         |        |    |

甚至

 user_id | price  | id |          email           
---------+--------+----+--------------------------
       5 | $50.00 |    | 
         |        |  2 | fahir@example.com

将是一个好的开始。

具体来说,我想知道如何从joins_example中选择具有内部联接中不存在的user_idid的行。

3 个答案:

答案 0 :(得分:0)

SELECT * 
FROM joins_example AS w 
LEFT JOIN (
    select x.user_id 
    from joins_example as x 
    inner join joins_example as y on x.user_id = y.id
) AS z ON z.user_id = w.user_id or z.user_id = w.id 
WHERE z.user_id IS NULL;

即是一个很好的开始

 user_id | price  | id |       email       | user_id 
---------+--------+----+-------------------+---------
       7 | $20.00 |    |                   |        
         |        |  2 | fahir@example.com |

答案 1 :(得分:0)

您可以考虑使用具有NOT EXISTS条件的相关子查询的方法:

select * 
from joins_example as x 
where 
    ( 
        x.user_id is not null 
        and not exists (
            select 1 from joins_example y where  x.user_id = y.id
        )
    )
    or ( 
        x.id is not null 
        and not exists (
            select 1 from joins_example y where  x.id = y.user_id
        )
    )

Demo on DB Fiddle

| user_id | price | id  | email             |
| ------- | ----- | --- | ----------------- |
| 7       | 20.00 |     |                   |
|         |       | 2   | fahir@example.com |

答案 2 :(得分:0)

SELECT *
FROM joins_example j
WHERE (j.user_id IS NULL AND j.id IS NULL)
    OR (j.user_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM joins_example j2 WHERE j2.id = j.user_id))
    OR (j.id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM joins_example j2 WHERE j2.user_id = j.id));