选择与用户属于同一组的所有行

问题描述 投票:-1回答:1

我有一个称为article的实体。用户属于一个组,例如company字段的值为MyCompany

article的作者用户电子邮件的外键为author_email

选择与用户属于同一company的所有文章而不在company上添加字段article的方法是什么?

sql postgresql
1个回答
0
投票

如果我理解正确,您可以使用CTE将公司分配给每篇文章,然后在查询中使用它:

with au as (
      select a.*, u.company
      from articles a join
           users u
           on a.author_email = u.email
     )
select au.*
from au
where au.company = (select u.company from users u where u.email = ?);
© www.soinside.com 2019 - 2024. All rights reserved.