简体   繁体   English

考虑来自其他表的数据的 SQL 查询

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM