I have a scenario where I need to match values in columns of one table with rows in another.
I have two tables: employee_id and id_entries.
employee_id has the following structure:
employee_id id_1 id_2
1 123 456
2 789 120
3 345 678
4 901 234
id_entries has the following structure:
employee_name id_number
John Doe 123
Jane Smith 789
John Doe 456
Jane Smith 120
I want to be able to match the employee_id field in the employee_id table to employee_name in the id_entries table where there is an entry for both id_1 and id_2.
For example
employee_id employee_name id_1 id_2
1 John Doe 123 456
2 Jane Smith 789 120
I've tried several different joins but haven't been able to bring back the correct results. Thanks in advance.
you need to join it to two instances of id_entries table, one for each id coluomn:
select emploee_id, a.emploee_name, id_1, id_2 form
employee_id inner join id_entries as a on employee_id.id_1 = a.id_num
inner join id_entries as b on employee_id.id_2 = b.id_num
where a.emploee_name = b.emploee_name
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments