我有这个查询,并且为了查询经理部门名称总是在标题中得到错误:
select e.employee_id e_id
, e.first_name || ' ' || e.last_name employee_name
, e.job_id
, e.department_id
, e.department_name
, e.salary || ' $ '
, to_char(e.hire_date , ' day- dd - mon -yyyy' ) employee_hire_date
, m.employee_id m_id
, m.first_name || ' ' || m.last_name manager_name
, m.job_id
, m.department_id
, m.department_name
, m.salary
FROM employees e
JOIN employees m
on m.employee_id = e.manager_id
JOIN departments d
on d.department_id = e.department_id
JOIN departments d
on d.department_id = m.department_id
答案 0 :(得分:1)
问题出现在这里:
JOIN departments d
on d.department_id = e.department_id
JOIN departments d
on d.department_id = m.department_id
要么删除它们中的一个,因为您没有从这些表中选择任何内容,employee
表中的所有内容都存在,或者它们的别名不同。
JOIN departments d
on d.department_id = e.department_id
JOIN departments dd
on dd.department_id = m.department_id
当您尝试使用具有相同名称/别名的两个不同列时,将发生此错误。每列必须有自己唯一的名称/别名。
答案 1 :(得分:0)
您使用了两次相同的别名。请尝试以下方法:
select e.employee_id e_id
, e.first_name || ' ' || e.last_name employee_name
, e.job_id
, e.department_id
, e.department_name
, e.salary || ' $ '
, to_char(e.hire_date , ' day- dd - mon -yyyy' ) employee_hire_date
, m.employee_id m_id
, m.first_name || ' ' || m.last_name manager_name
, m.job_id
, m.department_id
, m.department_name
, m.salary
FROM employees e join employees m
on m.employee_id = e.manager_id
JOIN departments d
on d.department_id = e.department_id
JOIN departments d2
on d2.department_id = m.department_id
答案 2 :(得分:0)
不同地区分部门名称并更正选择假设department_Id和部门名称来自部门。
select e.employee_id e_id
, e.first_name || ' ' || e.last_name employee_name
, e.job_id
, de.department_id --***This line Updated alias
, de.department_name --***this line Updated alias
, e.salary || ' $ '
, to_char(e.hire_date , ' day- dd - mon -yyyy' ) employee_hire_date
, m.employee_id m_id
, m.first_name || ' ' || m.last_name manager_name
, m.job_id
, dm.department_id --***This line Updated alias
, dm.department_name --***This line Updated alias
, m.salary
FROM employees e
JOIN employees m
on m.employee_id = e.manager_id
JOIN departments de --***this line Updated alias
on de.department_id = e.department_id --***this line Updated alias
JOIN departments dm --***this line Updated alias
on dM.department_id = m.department_id --***this line Updated alias