如何仅使用连接而不使用子查询来创建此查询?

时间:2012-07-19 10:18:24

标签: mysql

正如标题所暗示的那样,重新编写此内容的方法是将其作为连接排除?

select users.id from users where id not in ("select
pu.owner_id from projects p
inner join
projects_users pu on
pu.owned_project_id = p.id
where p.project_name = 'This is an example of a project'")

我认为这可能有效,但它没有从project_users中不存在的用户返回任何内容:

select u.id from users u
        left outer join projects_users pu on
        pu.owner_id = u.id
        inner join projects p on
        pu.owned_project_id = p.id          
        where NOT p.project_name = 'This is an example of a project'

1 个答案:

答案 0 :(得分:3)

SELECT    a.id
FROM      users a
LEFT JOIN project_users b ON a.id = b.owner_id
LEFT JOIN projects c ON b.owned_project_id = c.id AND 
          c.project_name = 'This is an example of a project'
WHERE     c.id IS NULL