[英]SQL query that considers data from other table
I have two tables:我有两张桌子:
employees
: employees
:
id, CMS_user_id, practice_group_id, ...
and和
users
: users
:
id, level, ...
I want to select all employees where practice_group_id
is 2 but only if the respective user has a level
of 1 according to the users
table.我想选择
practice_group_id
为 2 的所有员工,但前提是根据users
表,相应用户的level
为 1。 I researched and I have a feeling it has something to do with the UNION
keyword eventually, but I can't quite figure it out.我进行了研究,我感觉它最终与
UNION
关键字有关,但我无法弄清楚。
In "human language", the query would be like this: "select all from employees
where practice_group_id
is 2 and then check the CMS_user_id
from the employee and check in the table users
whether the respective user with the id
that equals CMS_user_id
has a level
of 1"在“人类语言”中,查询将是这样的:“从
practice_group_id
为 2 的employees
中选择所有,然后检查员工的CMS_user_id
并在表users
中检查id
等于CMS_user_id
的相应用户是否具有level
1"
A JOIN
will match the corresponding rows between two tables. JOIN
将匹配两个表之间的相应行。 Then, filtering can be done using WHERE
.然后,可以使用
WHERE
进行过滤。
For example:例如:
select e.*
from employees e
join users u on u.id = e.CMS_user_id
where e.practice_group = 2 and u.level = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.