我这里有一个查询,需要检索员工的 employeeid、姓名、开始日期、期间、经理、业务线、单位、子单位和状态
Employee 表连接到其他表,我提供了我需要的员工的 employeeid,我将它连接到另一个表,这样如果员工没有值,它只会返回 null。我没有得到我所期望的。我哪里出错了?
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
最佳答案
您的问题与 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 所有其他联接。
关于mysql - 左连接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32436986/