SQL 考虑其他 table 数据的查询

SQL query that considers data from other table

我有两个table:

employees:

id, CMS_user_id, practice_group_id, ...

users:

id, level, ...

我想 select practice_group_id 为 2 的所有员工,但前提是根据 users [=45=,相应用户的 level 为 1 ].我研究了一下,感觉它最终与 UNION 关键字有关,但我不太明白。

在“人类语言”中,查询将是这样的: “select 全部来自 employees,其中 practice_group_id 为 2,然后检查员工的 CMS_user_id 并检查 table users 是否各自id 等于 CMS_user_id 的用户的 level 为 1"

A JOIN 将匹配两个表之间的相应行。然后,可以使用 WHERE.

进行过滤

例如:

select e.*
from employees e
join users u on u.id = e.CMS_user_id
where e.practice_group = 2 and u.level = 1