在创建返回列出每个员工及其经理的结果集的查询时遇到问题。
如果您熟悉员工数据库,您就知道他们是一个员工表、一个 dept_manager 表和一个 dept_emp 表,您可以在其中链接前两个表。
我的结果集只为每个人和一个部门返回一位经理。
我很感激我能得到的任何帮助!
SELECT d.dept_name AS 'Dept',
CONCAT(em.last_name, ' ', em.first_name) AS 'Manager last, first',
CONCAT(e.last_name,' ', e.first_name, ' ', t.title) AS 'Employee last, first (title)'
FROM dept_manager AS dm
LEFT JOIN dept_emp AS de ON de.dept_no = dm.dept_no
LEFT JOIN departments AS d ON d.dept_no = dm.dept_no
LEFT JOIN employees AS e ON e.emp_no = de.emp_no
LEFT JOIN employees AS em ON em.emp_no = dm.emp_no
LEFT JOIN titles AS t ON t.emp_no = e.emp_no
WHERE
dm.emp_no = e.emp_no
AND
dept_name = 'Sales'
OR
dept_name = 'Marketing'
AND
dm.to_date >= '2012-05-07'
AND
t.to_date > '2012-05-07'
AND
de.to_date > '2012-05-07'
ORDER BY e.last_name, e.first_name
limit 1000