![](/img/trans.png)
[英]Select from two tables only that rows that satisfies multiple conditions in second table
[英]Get a list of all rows from one table, that satisfies multiple criteria in two different tables
我有4個表employee
, company
, employee_company
和action
。
employee_company
包含employee_id
和company_id
,而action
除其他外還包含employee_id
。
或者, employee_company
對employee
和company
具有外鍵,而action
對employee
則具有外鍵。
我正在嘗試找出一種方法來獲取屬於擁有多名雇員的公司的所有雇員的列表,並且該公司中沒有雇員在action
表中有任何條目。
我將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子句-可以過濾掉帶有操作的員工,因為在沒有聯接的情況下,左聯接的行具有全空列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.