我们可以在左连接的ON子句中写条件可能满足也可能不满足?

时间:2016-04-06 07:53:00

标签: mysql sql join left-join

我有一个审计跟踪表,可以保存所有已执行的记录,例如添加更新和删除项目。 在那里,我维护一个列,用于保存执行操作的多个表的主键。这是整数列

我的查询就像这样

select * from 
user usr1  
left join activity_history 
on activity_history.userID= usr1.sequenceID

left join candidate can1 on can1.userID = usr1.sequenceID  
and can1.userID = activity_history.activity_sequenceID

left join institute ins1 on ins1.userID= usr1.sequenceID  
and ins1.userID = activity_history.activity_sequenceID

left join candidate_institutes caninst on caninst.candidateID = can1.candidateID and caninst.instituteID= ins1.instituteID

left join exam exam1 on exam1.instituteID = ins1.instituteID 
and exam1.examID = activity_history.activity_sequenceID

left join proctor pro1 on pro1.userID = usr1.sequenceID 
and pro1.proctorID = activity_history.activity_sequenceID

left join appointment appt1  on  appt1.examID = exam1.examID
  and appt1.sequenceID = activity_history.activity_sequenceID

/*  COMMENTED CODE-----

 on(  activity_history.activity_sequenceID=can1.userID
 OR activity_history.activity_sequenceID=ins1.userID
OR activity_history.activity_sequenceID=exam1.examID  and activity_history.userID= usr1.sequenceID
 OR activity_history.activity_sequenceID=pro1.userID
 OR activity_history.activity_sequenceID=appt1.sequenceID
  )
*/

order by activity_history.sequenceID desc

activity_sequenceID是我存储其他表的键的列 我需要映射它。这是加入这些表的正确方法还是评论部分可能是礼仪方式? 或者有没有其他方法来加入这些表。 我很困惑,因为我正在写一个条件,但activity_histry表可能有也可能没有该特定表的记录。

0 个答案:

没有答案