1

我正在使用mysql。

我有两张桌子

Students:
stud_num 
prof1
prof2

Prof
prof_id
first_name
last_name

Prof Id 是学生表中的外键。

我想将 stud_num 和 prof1 作为名字和姓氏,将 prof2 作为名字和姓氏

这是我的查询不起作用:

select s.stud_num, CONCAT(p.first_name, " ", p.last_name) as PROF1, CONCAT(p.first_name, " ", p.last_name) as PROF2
from students s
inner join prof p
on s.prof1 = p.prof
and s.prof2 = p.prof

这不起作用。是否有捷径可寻???

4

3 回答 3

3

加入 TWICE 到教授表,但我建议 LEFT 加入,以防没有提供教授的 ID 之一

select 
      s.stud_num, 
      CONCAT(p1.first_name, " ", p1.last_name) as PROF1, 
      CONCAT(p2.first_name, " ", p2.last_name) as PROF2
   from 
      students s
         LEFT join prof p1
            on s.prof1 = p1.prof
         LEFT join prof p2
            on s.prof2 = p2.prof
于 2012-04-19T14:47:36.210 回答
3

Prof 表需要以两个不同的名称连接两次。尝试这个:

select s.stud_num,
       CONCAT(p1.first_name, " ", p1.last_name) as PROF1,
       CONCAT(p2.first_name, " ", p2.last_name) as PROF2
from   students s,
       prof p1,
       prof p2
where  s.prof1 = p1.prof_id
and    s.prof2 = p2.prof_id
于 2012-04-19T14:48:37.327 回答
1

请尝试以下方法:

SELECT s.stud_num, 
       CONCAT(p1.first_name, " ", p1.last_name) as PROF1, 
       CONCAT(p2.first_name, " ", p2.last_name) as PROF2
FROM students s, prof p1, prof p2
WHERE s.prof1 = p1.prof
      AND s.prof2 = p2.prof
于 2012-04-19T14:45:55.917 回答