从数据库中选择两个ID

时间:2015-08-12 18:44:48

标签: mysql

我有一个关于从Mysql表中选择的问题。 我有两张桌子,第一张桌子是Project:
项目

ID name        student1   student2


1  delete       1            2

其他表是学生表: 学生

studentID name


1          Tom


2          Nina

如何选择哪个可以生成这样的表?

Projectname student1  student1name student2 student2name
delete        1             Tom         2         Nani

1 个答案:

答案 0 :(得分:1)

可能类似于(sqlfiddle

SELECT 
    p.ProjectName, s1.studentId student1, s1.Name student1name, s2.studentId student2, s2.Name student2name
FROM   
    Project p 
    INNER JOIN Student s1 ON p.student1 = s1.studentId
    INNER JOIN Student s2 ON p.student2 = s2.studentId