SQL查询-部门和员工表,某些部门可能没有经理
有两个表格:SQL查询-部门和员工表,某些部门可能没有经理,sql,Sql,有两个表格: Employee --------- emp_id emp_name emp_contact salary mgr_emp_Id Dept_No Dept ----- Dept_No Dept_name Dept_Location 编写SQL语句,列出所有部门名称以及位置,如果已将经理分配给该部门,还列出经理姓名。请注意,有些部门仍然没有经理。 以下是正确的吗 SELECT Dept_name, Dept_Location, emp_name AS Mgr_name
Employee
---------
emp_id
emp_name
emp_contact
salary
mgr_emp_Id
Dept_No
Dept
-----
Dept_No
Dept_name
Dept_Location
编写SQL语句,列出所有部门名称以及位置,如果已将经理分配给该部门,还列出经理姓名。请注意,有些部门仍然没有经理。
以下是正确的吗
SELECT Dept_name, Dept_Location, emp_name AS Mgr_name
FROM Dept
LEFT JOIN Employee ON (Dept.Dept_No = Employee.Dept_No AND mgr_emp_id = emp_id)
不加入也能做到这一点吗?如果是,如何实现?是的,即使我更喜欢使用联接,您也可以在不使用联接的情况下实现这一点。必须修改您的查询。以下是如何 例子 (MySQL 5.5) (SQL Server 2008) (PostgreSQL 9.2) 假设 1) 在员工表中:经理id+部门id将是唯一的 2) 在employee表中:如果employee Clark的ID为5,并且有一个ID为1的经理,则该表中将有一条ID为1的记录 3) 正在使用MySQL 5.5 结构 MySQL 5.5查询 无连接
select
list.*,
emp_id,
emp_name
from employee,
(
select
distinct
dept.dept_no,
dept.dept_name,
dept.dept_location,
employee.mgr_emp_id
from dept, employee
where
dept.dept_no = employee.dept_no
and employee.mgr_emp_id is not null
) list
where
employee.emp_id = list.mgr_emp_id;
select
dept.dept_no,
dept.dept_name,
dept.dept_location,
employee.mgr_emp_id
from dept, employee
where
dept.dept_no = employee.dept_no
and employee.mgr_emp_id is not null;
select
dept.dept_no,
dept.dept_name,
dept.dept_location,
employee.mgr_emp_id
from dept
left join employee on dept.dept_no = employee.dept_no
where
employee.mgr_emp_id is not null;
使用JOIN(虽然与上面的不完全相同。我更喜欢使用JOIN
s)
它是如何工作的
首先,我们希望在employee表中获得经理ID为NOTNULL的所有部门的列表。为此,我们使用下面的查询。此查询将提供芝加哥的2条记录,因为employee表中有2条记录具有芝加哥部门的有效经理ID
无连接
select
list.*,
emp_id,
emp_name
from employee,
(
select
distinct
dept.dept_no,
dept.dept_name,
dept.dept_location,
employee.mgr_emp_id
from dept, employee
where
dept.dept_no = employee.dept_no
and employee.mgr_emp_id is not null
) list
where
employee.emp_id = list.mgr_emp_id;
select
dept.dept_no,
dept.dept_name,
dept.dept_location,
employee.mgr_emp_id
from dept, employee
where
dept.dept_no = employee.dept_no
and employee.mgr_emp_id is not null;
select
dept.dept_no,
dept.dept_name,
dept.dept_location,
employee.mgr_emp_id
from dept
left join employee on dept.dept_no = employee.dept_no
where
employee.mgr_emp_id is not null;
与JOIN一起
select
list.*,
emp_id,
emp_name
from employee,
(
select
distinct
dept.dept_no,
dept.dept_name,
dept.dept_location,
employee.mgr_emp_id
from dept, employee
where
dept.dept_no = employee.dept_no
and employee.mgr_emp_id is not null
) list
where
employee.emp_id = list.mgr_emp_id;
select
dept.dept_no,
dept.dept_name,
dept.dept_location,
employee.mgr_emp_id
from dept, employee
where
dept.dept_no = employee.dept_no
and employee.mgr_emp_id is not null;
select
dept.dept_no,
dept.dept_name,
dept.dept_location,
employee.mgr_emp_id
from dept
left join employee on dept.dept_no = employee.dept_no
where
employee.mgr_emp_id is not null;
要仅获取一条记录,我们将使用distinct关键字:
select
distinct
dept.dept_no,
...
太好了,现在我们知道每个部门的经理是谁了。让我们找到这个人的名字。为此,我们将查询放入一个子查询(我将其昵称/别名为
list
),然后将其与employee表组合以获得所需的结果。这可能不正确。但是,如果没有示例数据,很难判断。那么,您是否尝试运行它并查看结果?我尝试使用非常简单的数据-在Dept表中添加了2行,但在employee表中只添加了1行,这一行对应于其中一个部门。我得到了正确的结果,即对于一个部门,我得到了员工表中的经理姓名,而对于另一个部门,经理姓名为空。但我不确定它是否在所有情况下都有效。