包含多个联接的复杂查询未执行正确的外部联接

时间:2019-01-23 07:56:04

标签: mysql sql sql-server database

我正在编写一个具有多个联接的查询,我希望每个用户显示针对所有类别类型的条目。当我在下面执行查询时,仅返回1条记录,因为员工u.employee_id = "0079-P"仅从事过1个项目,但是我想获取所有category_types的数据,而用户工作时间对于他所做的类别显示为空不努力。

Select u.employee_id As Employee_ID, u.user_name As UserName, COALESCE(primaryDept.ctd_name, primaryProj.ctd_name) As PrimaryDeptOrProj, region.region_name As Region, categoryType.ctd_id, categoryType.ctd_name, SUM(tsdd.workhours) 
    From users u 
    LEFT JOIN category_type_details primaryDept ON u.user_primary_department = primaryDept.ctd_id
    LEFT JOIN category_type_details primaryProj ON u.user_primary_project = primaryProj.ctd_id
    LEFT JOIN regions region ON u.region_id = region.region_id
    LEFT JOIN timesheets ts ON u.user_id = ts.timesheet_user
    INNER JOIN timesheet_mr tsmr ON ts.timesheet_caller = tsmr.tsmr_id
    INNER JOIN timesheet_details tsd ON ts.timesheet_id = tsd.tsd_timesheet_id
    INNER JOIN timesheet_day_details tsdd ON tsd.tsd_id = tsdd.tsd_id
    RIGHT OUTER JOIN category_type_details categoryType ON tsd.tsd_category_type_id = categoryType.ctd_id
    WHERE tsmr.tsmr_id = 14 and u.employee_id = "0079-P"
    GROUP BY u.user_id, tsd.tsd_category_type_id;

我尝试了此查询的变体形式,无论如何都会返回1条记录。

1 个答案:

答案 0 :(得分:0)

您可以将查询更改为此:

SELECT u.employee_id AS Employee_ID, u.user_name AS UserName, 
    COALESCE(pd.ctd_name, pp.ctd_name) AS PrimaryDeptOrProj, 
    r.region_name AS Region, ct.ctd_id, ct.ctd_name, SUM(tsdd.workhours) 
FROM users u 
LEFT JOIN category_type_details pd ON u.user_primary_department = pd.ctd_id
LEFT JOIN category_type_details pp ON u.user_primary_project = pp.ctd_id
LEFT JOIN regions r ON u.region_id = r.region_id
LEFT JOIN timesheets ts ON u.user_id = ts.timesheet_user
INNER JOIN timesheet_mr tsmr ON ts.timesheet_caller = tsmr.tsmr_id AND tsmr.tsmr_id = 14
INNER JOIN timesheet_details tsd ON ts.timesheet_id = tsd.tsd_timesheet_id
INNER JOIN timesheet_day_details tsdd ON tsd.tsd_id = tsdd.tsd_id
RIGHT OUTER JOIN category_type_details ct ON tsd.tsd_category_type_id = ct.ctd_id AND u.employee_id = "0079-P"
GROUP BY ct.ctd_id, u.user_id, u.employee_id, u.user_name, 
    COALESCE(pd.ctd_name, pp.ctd_name), r.region_name, ct.ctd_name
ORDER BY ct.ctd_id, u.user_id, u.employee_id, u.user_name, 
    COALESCE(pd.ctd_name, pp.ctd_name), r.region_name, ct.ctd_name;

您只有1行,因为WHERE子句中的条件过滤了其他类别的所有NULL user_id行。

对于MySQL,您可以省略GROUP BY子句中的其他列:

SELECT u.employee_id AS Employee_ID, u.user_name AS UserName, 
    COALESCE(pd.ctd_name, pp.ctd_name) AS PrimaryDeptOrProj, 
    r.region_name AS Region, ct.ctd_id, ct.ctd_name, SUM(tsdd.workhours) 
FROM users u 
LEFT JOIN category_type_details pd ON u.user_primary_department = pd.ctd_id
LEFT JOIN category_type_details pp ON u.user_primary_project = pp.ctd_id
LEFT JOIN regions r ON u.region_id = r.region_id
LEFT JOIN timesheets ts ON u.user_id = ts.timesheet_user
INNER JOIN timesheet_mr tsmr ON ts.timesheet_caller = tsmr.tsmr_id AND tsmr.tsmr_id = 14
INNER JOIN timesheet_details tsd ON ts.timesheet_id = tsd.tsd_timesheet_id
INNER JOIN timesheet_day_details tsdd ON tsd.tsd_id = tsdd.tsd_id
RIGHT OUTER JOIN category_type_details ct ON tsd.tsd_category_type_id = ct.ctd_id AND u.employee_id = "0079-P"
GROUP BY ct.ctd_id, u.user_id
ORDER BY ct.ctd_id, u.user_id;