2015-09-07 65 views
3

我這裏有查詢需要檢索的僱員,名字,開始日期,週期,經理,bussinessline,單位,員工的β亞基和狀態LEFT JOIN不正常

Employee表連接到其他表,我提供僱員的僱員id我需要有我的名單,我留下來加入到另一張桌子,這樣如果僱員沒有值,它只會返回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 
,11
,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 
+1

左加入所有其他連接以及 –

+0

更改被分配的條件並將其放在他的表下 – user3600910

+0

另外:既然os_form.employee_id = employee.id,並且您想要一個左連接,請使用:where employee.id in ...)而不是os_form.employee_id在(...) –

回答

0

因爲你POY的條件下,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 
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所有其他加入爲好。

+0

嗨,感謝您的回答,但可悲的是,仍然無法正常工作。 檢查我的編輯謝謝 – Dave