SELECT IF(t1.AccountNo IS NULL, t2.TempAccountNo, t2.AccountNo) AS optional_field
FROM table2 t2
LEFT JOIN t1 ON t1.AccountNo = t2.AccountNo
SELECT IF(t1.AccountNo IS NULL, t2.TempAccountNo, t2.AccountNo) AS optional_field
FROM table2 t2
LEFT JOIN t1 ON t1.AccountNo = t2.AccountNo