简体   繁体   中英

Postgres - Left Join, same table

I have a table of subscription records for people subscribed to groups. There's a groupid column and a userid column (in addition to the primary key id). I want to find all the subscription rows of people subscribed to group 1 that do not also have a subscription record for group 2. I thought I could use an anti-join pattern, but I can't get it to work. I've tried:

SELECT
*
FROM subs s1
LEFT JOIN subs s2 ON s1.groupid=1 AND s2.groupid=2 AND s1.userid=s2.userid
WHERE s2.id IS NULL;

But that does not work (it returns subscription records with groupids that are not 1).

I would use group by for this:

select s.userid
from subs s
group by s.userid
having sum(case when s.groupid = 1 then 1 else 0 end) > 0 and
       sum(case when s.groupid = 2 then 1 else 0 end) = 0;

Each condition in the having clause counts records for one of the groups. The first condition says there is at least one row with groupid = 1 . The second says there are no records with groupid = 2 .

You can do what you want with a left join as well. Here is the approach:

SELECT s1.*
FROM subs s1 LEFT JOIN
     subs s2
     ON s1.userid = s2.userid AND s2.groupid = 2
WHERE  s1.groupid = 1 AND s2.id IS NULL;

Because it is a left join , the condition on the first table should go in the where clause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM