从满足两个不同 table 中的多个条件的一个 table 中获取所有行的列表

Get a list of all rows from one table, that satisfies multiple criteria in two different tables

我有 4 个 tables - employeecompanyemployee_companyaction.

employee_company 包含 employee_idcompany_id,而 action 包含 employee_id.

或者,employee_company 有指向 employeecompany 的外键,而 action 有指向 employee 的外键。

我正在尝试找出一种方法来获取属于拥有多个员工的公司的所有员工的列表,并且该公司的任何员工都没有条目 action table.

我将 sequelize.js 用作 orm,因此原始 SQL 或通过 sequelize 以某种方式进行操作都会有所帮助。

使用子查询查找拥有多名员工的公司,并使用左联接操作:

select e.*
from (select company_id
      from employee_company
      group by conpany_id
      having count(*) > 1) c
join employee_company ec on ec.company_id = c.company_id
join employee e on ec.employee_id = e.id
left join action a on a.employee_id = e.id
where a.employee_id is null

这应该是合理的不言自明的,也许 where 子句除外 - 过滤掉具有操作的员工,因为在没有连接时左连接行具有全空列。