如何使用c#winform连接一个表,该表有两个外键链接到另一个表中的主键 [英] how to join a table that has two foreign keys linked to the primary key in the other table using c# winform

查看:192
本文介绍了如何使用c#winform连接一个表,该表有两个外键链接到另一个表中的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们

请给我一些建议,我已经在这个问题上待了很久。我有两个表工作人员和学生表上的学生表我有列,如姓名,referStaffID,BackUpStaffID,年龄,员工表有staffID,名称和部门。我从student表创建了两个外键,即student_StaffReferringStaff,它将ReferStaffID链接到staff表中的staffID,还有另一个外键student_StaffBackupStaff,它将BackupStaffID链接到staff表中的staffID。我现在的问题是加入学生桌和工作人员表,并获得推荐的员工姓名和备份员工姓名。我需要你的建议..

提前致谢

解决方案

你只需要加入两次,一次是为推荐人员而另一次是为了备份人员。



例如:< br $>


  SELECT  s.Name  AS  StudentName,r.Name  AS  ReferStaffName,b.Name  AS  BackupStaffName 
FROM 学生s
LEFT JOIN staff r ON s.referringStaffID = r.staffID
LEFT JOIN staff b ON s.BackUpStaffID = b.sta FFID


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

解决方案

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

For example:

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


这篇关于如何使用c#winform连接一个表,该表有两个外键链接到另一个表中的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆