[英]How to get all columns from one table and only one column from another table with ID ? - MySql
I have table employee which contains about 9 columns . 我有员工表,其中包含约9列。 ' id,name,etc..' 'id,名称等。'
and I have another table 'onCall' contain 3 columns 'employee_id,department_id and rank ' 我还有另一个表“ onCall”,其中包含3列“ employee_id,department_id和等级”
what I want is to retrieve the employee data who is registered as OnCall employee on this department 我想要的是检索在此部门注册为OnCall员工的员工数据
I try this to get the employee data : 我尝试这样做以获得员工数据:
Select * from employee where id in (SELECT employee_id FROM onCall where department_id = 3)
But like this I can't know what is the rank of the onCall employee , is he registered as primary or backup ,how can I merge rank column from onCall table but only for the selected employee by the id 但是这样我不知道onCall员工的等级是多少,他是注册为主要员工还是后备员工,我该如何合并onCall表中的等级列,但仅按ID对所选员工进行合并
I tried to join them but I get syntax error 我尝试加入他们,但出现语法错误
any way to solve this ? 有什么办法解决这个问题?
This may help you and use the required columns with table alias name 这可能会对您有所帮助,并使用带有表别名的必需列
Select e.*, o.* from employee e
LEFT JOIN onCall o ON o.employee_id = e.id
AND o.department_id = 3
This calls for an inner join 这需要内部联接
select EMP.*, OC.*
from EMPLOYEE EMP
inner join ONCALL OC
on OC.EMPLOYEE_ID = EMP.ID
where OC.DEPARTMENT = 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.