SELECT IFNULL(b.referenceNo, a.referrence) refNo, a.*
FROM (`comp_journal` a)
LEFT JOIN `view_journal_table` b ON `b`.`journalID` = `a`.`journalID` and b.formNo = a.formNo and b.companyID = a.companyID
WHERE `a`.`companyID` =  '3'
AND `a`.`year` =  '2016'
AND `a`.`period` =  '02'
AND `a`.`status` =  0
AND `a`.`is_delete` =  0