Click here to Skip to main content
15,886,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends
please kindly give me suggestions, i have been on this issue for long now. i have two tables staffs and students tables on student table i have columns like Name,referringStaffID, BackUpStaffID, Age, and the Staffs table has staffID, Name, and department. i created two foreign keys from the student table which are student_StaffReferringStaff which links the ReferringStaffID to the staffID in the staff table and also another foreign key student_StaffBackupStaff which links the BackupStaffID to staffID in the staff table. my problem now is joining the the student table with the staff table and getting the the referring Staff Name and the Backup Staff Name.please i need your suggestions..
Thanks in advance
Posted
Comments
Ese Ochuko 20-Aug-14 11:56am    
sorry am using sql server database

1 solution

You just need to join twice, once for the referring staff and once for the backup staff.

For example:

SQL
SELECT s.Name AS StudentName, r.Name AS ReferringStaffName, b.Name AS BackupStaffName
FROM students s
LEFT JOIN staff r ON s.referringStaffID = r.staffID
LEFT JOIN staff b ON s.BackUpStaffID = b.staffID
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900