左连接不起作用

时间:2015-09-07 10:57:07

标签: mysql join left-join

我在这里查询需要检索员工,姓名,开始日期,期间,经理,商务线,单位,子单位和员工的状态

员工表已连接到其他表,我提供了我需要的员工的员工ID,我将其加入到另一个表中,这样如果员工没有这些值,它将只返回空值。我达不到我的预期。我哪里出错了?

 select employee.id as 'Employee ID', concat_ws(' ',employee.first_name, employee.last_name) as 'Name', 
date_started as 'Start date', os_form.os_event_id as 'Period', 
(select concat_ws(' ',employee.first_name, employee.last_name) from employee where id = os_form.created_by) as 'Manager', 
business_line.name as 'Business_line', unit.name as 'Unit', sub_unit.name as 'Sub Unit', os_form.status as 'Status'
from employee
LEFT JOIN os_form 
    ON (os_form.employee_id = employee.id and os_form.employee_id 
    IN (110013
,110015
,110002
,110042
,110057
,110065
,110060
,110062
,110092
,110072
,110095
,110100
,110104
,110106
,110107
,110109
,110077
,110114
,110117
,110121
,110123
,110073
,110128
,110138
,110147
,110153
,110162
,110173
,110178
,110183
,110211
,110215
,110216
,110218
,110219
,110224
,110226
,110230
,110245
,110249
,110257
,110260
,110262
,110264
,110266
,110268
,110269
,110272
,110275
,120076
,120083
,120090
,120099
,120095
,120101
,120108
,120109
,120110
,120112
,120119
,120127
,120128
,120132
,120133
,120134
,120145
,120146
,120142
,130010
,130012
,130016
,130017
,130018
,130020
,130021
,130006
,130034
,130035
,130038
,130041
,130029
,130046
,130047
,130056
,130013
,130027
,130028
,130030
,130037
,130040
,130043
,130044
,130048
,130049
,130054
,130055
,130031
,130050
,130064
,130065
,130084
,130092
,130094
,130095
,130096
,130097
,130098
,130099
,130105
,130051
,130059
,130111
,130112
,130115
,130116
,130127
,130136
,130109
,140002
,140105
,140158
,140178
,140304
,140171
,140358
,140352
,140354
,140356
,140401
,140407
,140410
,140411
,140409
,140412
,140417
,140418
,140419
,140422
,140423
,150003
,150132
,150141
,150143
,150167
,150193
,150199))
left join business_line on os_form.business_line_id = business_line.id
left join unit on unit.id = os_form.unit_id
left join sub_unit on sub_unit.id = os_form.sub_unit_id
WHERE os_event_id = 2015
group by employee.id

2 个答案:

答案 0 :(得分:2)

您的问题出在WHERE声明中。更准确地说,您检查标识符列表中是否存在os_form.employee_id这一事实。

您应该将该支票移至LEFT JOIN ... ON ()

LEFT JOIN os_form 
    ON (os_form.employee_id = employee.id and os_form.employee_id IN (110013,110015, ...))

或将其保留在WHERE中并检查NULL:

WHERE (os_form.employee_id IS NULL or os_form.employee_id IN
(
  110013,110015,110002,110042,110057,110065,110060,110062,110092,110072,110095,110100,110104,110106,110107,110109,110077,110114,110117,110121,110123,110073,110128,110138,110147,110153,110162,110173,110178,110183,110211,110215,110216,110218,110219,110224,110226,110230,110245,110249,110257,110260,110262,110264,110266,110268,110269,110272,110275,120076,120083,120090,120099,120095,120101,120108,120109,120110,120112,120119,120127,120128,120132,120133,120134,120145,120146,120142,130010,130012,130016,130017,130018,130020,130021,130006,130034,130035,130038,130041,130029,130046,130047,130056,130013,130027,130028,130030,130037,130040,130043,130044,130048,130049,130054,130055,130031,130050,130064,130065,130084,130092,130094,130095,130096,130097,130098,130099,130105,130051,130059,130111,130112,130115,130116,130127,130136,130109,140002,140105,140158,140178,140304,140171,140358,140352,140354,140356,140401,140407,140410,140411,140409,140412,140417,140418,140419,140422,140423,150003,150132,150141,150143,150167,150193,150199
))

您可能还需要LEFT JOIN所有其他连接。

答案 1 :(得分:0)

由于你在这个地方下的条件

尝试这个

select employee.id as 'Employee ID', concat_ws(' ',employee.first_name, employee.last_name) as 'Name', 
date_started as 'Start date', os_form.os_event_id as 'Period', 
(select concat_ws(' ',employee.first_name, employee.last_name) from employee where id = os_form.created_by) as 'Manager', 
business_line.name as 'Business_line', unit.name as 'Unit', sub_unit.name as 'Sub Unit', os_form.status as 'Status'
from employee
left join os_form on os_form.employee_id = employee.id
and os_form.employee_id in
(
110013
,110015
,110002
150193
,150199
)
join business_line on os_form.business_line_id = business_line.id
join unit on unit.id = os_form.unit_id
join sub_unit on sub_unit.id = os_form.sub_unit_id
and os_event_id = 2015
group by employee.id