【问题标题】:Left join not functioning左连接不起作用
【发布时间】:2015-12-02 21:50:36
【问题描述】:

我这里有一个查询,需要检索员工的员工 ID、姓名、开始日期、期间、经理、业务线、单位、子单位和状态

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

【问题讨论】:

  • 左连接所有其他连接
  • 将条件更改为并放在他的桌子下
  • 另外:由于 os_form.employee_id = employee.id,并且您想要左连接,请使用: where employee.id in (...) 而不是 os_form.employee_id in (...)

标签: mysql join left-join


【解决方案1】:

您的问题在于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 所有其他联接。

【讨论】:

  • 您好,感谢您的回答,但遗憾的是,仍然无法正常工作。检查我的编辑谢谢
【解决方案2】:

既然你在 WHERE 下设置条件

试试这个

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

【讨论】:

    猜你喜欢
    • 2012-09-10
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多