Rewriting Not In Sub-Select as Join for Propel

故事扮演 提交于 2019-12-02 07:40:56

问题


Given the following schema:

person:
  id: ~
group:
  id: ~
group_membership:
  person_id: ~
  group_id: ~

I am attempting to find members not within a certain group using Propel's Criteria, which the following SQL will do:

SELECT * FROM person
WHERE id NOT IN (
  SELECT person_id FROM group_membership
  WHERE group_id = 1
);

Unfortunately, Propel doesn't support sub-selects. It's possible to perform the sub-select first and pass it directly as an array, but I would rather do it in one call. I found this article, which suggests using a custom criteria or converting it to a join.

Is it possible to convert the above SQL to a single Join with no nested selects?


回答1:


I think this may be a substitute for the sub-query

SELECT *
FROM person
LEFT OUTER JOIN group_membership
  ON person.id = group_membership.person_id
   AND group_id = 1
WHERE group_membership.person_id is null
;

Rows returned where the person_id is null indicate where rows exist in person but not in group_membership



来源:https://stackoverflow.com/questions/6877226/rewriting-not-in-sub-select-as-join-for-propel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!