rookiecoder
05-20-2009, 01:57 PM
Hi, I thought I had a simple Left Join/Ifnull solution to my problem but it is taking me ages to figure out . As of now I've the following query from where I need to extract the employee's name and latest rank as per his last contract
SELECT employee.id,employee.name,rank_name.name
FROM employee,contract,rank_name
WHERE contract.employee_id=employee.id AND contract.rank=rank_name.rank
AND contract.start_date=(SELECT MAX(contract.start_date) FROM contract WHERE contract.employee_id=employee.id)
ORDER BY employee.name;
rank_name is an enumeration of all the ranks. So the above seems to be working fine , except that employees who have no contract records (hence no ranks) are not retrieved. What would be the best way to use Left Join ? From the few examples on web, I have only been able to see Left Join being used on a pair of tables, however here I have 3 tables
SELECT employee.id,employee.name,rank_name.name
FROM employee,contract,rank_name
WHERE contract.employee_id=employee.id AND contract.rank=rank_name.rank
AND contract.start_date=(SELECT MAX(contract.start_date) FROM contract WHERE contract.employee_id=employee.id)
ORDER BY employee.name;
rank_name is an enumeration of all the ranks. So the above seems to be working fine , except that employees who have no contract records (hence no ranks) are not retrieved. What would be the best way to use Left Join ? From the few examples on web, I have only been able to see Left Join being used on a pair of tables, however here I have 3 tables