简体   繁体   English

如何从一个表中获取所有列,而从另一张表中仅获取具有ID的一列? -MySQL

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

相关问题 如何使用mysql将一个表中的所有列集成到另一个表的一列中 - how to integrate all columns from one table into one column of another table using mysql 如何将一列连接到另一个表中的所有列 - how to join one column to all columns from another table 将一个表中的所有列插入到另一个表中 - Insert All Columns from one table into another table MYSQL 从一个表中获取不在另一个表中的所有数据 MySQL - Get all data from one table that's not in another table MySQL PHP MySQL:从一列获取数据,但使用单独表中不同列的id - PHP MySQL: Get data from one column but by using id from different columns in separate table mysql-表的两列和另一列的计数 - mysql - count of two columns of a table and one column from another 在mysql中,我如何只从一个表中获取行,而该行不链接到具有特定ID的另一表中的任何行 - In mysql how can I get only rows from one table which do not link to any rows in another table with a specific ID 如何从一个表数据复制列到另一表并在mysql中同时插入更多列? - how to copy column from one table data to another table and insert more columns same time in mysql? MySQL:如何将没有ID的一个表中的列复制到另一个表中? - MySQL: How to copy column from one table without id into another table? MySQL-如何从另一个表控制一个表的“数字”列 - MySQL - How to control `numeric` column of one table from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM