無法創建查詢,該查詢返回列出每個員工及其經理的結果集。mysql樣本員工數據庫 - 返回員工及其經理的查詢
如果您熟悉員工數據庫,您知道他們是員工表,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
我不熟悉員工db ... – quickshiftin 2013-05-08 05:54:39