LEFT JOIN语法的CASE条件

时间:2016-10-04 12:08:28

标签: mysql join left-join case

在回答这个问题后,在一组左连接中获取CASE将不再烦人!

然而,我有以下的查询和平是这是一个正确的apparoache应用一个case语句,在case语句(最后的括号)之前给出一个语法错误,说错过了右括号

from((((((`tbl_workflow_step_item`
LEFT JOIN `tbl_workflow_step_users` ON ((`tbl_workflow_step_item`.`step_id` = `tbl_workflow_step_users`.`step_id`)))
LEFT JOIN `tbl_workflow_steps` ON ((`tbl_workflow_step_item`.`step_id` = `tbl_workflow_steps`.`step_id`)))
LEFT JOIN `tbl_workflow_user_assigned` ON ((`tbl_workflow_step_users`.`user_id` = `tbl_workflow_user_assigned`.`user_id`)))
LEFT JOIN `tbl_workflow` ON ((`tbl_workflow`.`workflow_id` = `tbl_workflow_steps`.`workflow_id`)))
LEFT JOIN `tbl_report_type` ON ((`tbl_report_type`.`type_id` = `tbl_workflow_step_item`.`type`)))
case
when (`tbl_workflow`.`type` = 2) 
then 
left join `tbl_report_file` on ((`tbl_report_file`.`file_id`=`tbl_workflow_step_item`.`target_item_id`))) 
else
LEFT JOIN `tbl_blog` ON ((`tbl_blog`.`blog_id` = `tbl_workflow_step_item`.`target_item_id`)))

1 个答案:

答案 0 :(得分:1)

不,它不好。 Case / when语句不能在JOIN部分使用。这是你应该做的:

SELECT 
 CASE
    WHEN(`tbl_workflow`.`type` = 2) 
      THEN `tbl_report_file`.YOURFIELDHERE
    ELSE
    `tbl_blog`.YOURFIELDHERE
 END AS theAliasYouWant
FROM((((((`tbl_workflow_step_item`
LEFT JOIN `tbl_workflow_step_users` ON ((`tbl_workflow_step_item`.`step_id` = `tbl_workflow_step_users`.`step_id`)))
LEFT JOIN `tbl_workflow_steps` ON ((`tbl_workflow_step_item`.`step_id` = `tbl_workflow_steps`.`step_id`)))
LEFT JOIN `tbl_workflow_user_assigned` ON ((`tbl_workflow_step_users`.`user_id` = `tbl_workflow_user_assigned`.`user_id`)))
LEFT JOIN `tbl_workflow` ON ((`tbl_workflow`.`workflow_id` = `tbl_workflow_steps`.`workflow_id`)))
LEFT JOIN `tbl_report_type` ON ((`tbl_report_type`.`type_id` = `tbl_workflow_step_item`.`type`)))
LEFT JOIN `tbl_report_file` on ((`tbl_report_file`.`file_id`=`tbl_workflow_step_item`.`target_item_id`)))
LEFT JOIN `tbl_blog` ON ((`tbl_blog`.`blog_id` = `tbl_workflow_step_item`.`target_item_id`)))