什么是SQL查询?

时间:2015-08-03 09:27:21

标签: mysql sql

staff表:

      id     name
       1     tony
       2     sony
       3     bony
       4     rony
etc.

xyz表:

current staff, next staff, hod staff, vp staff.
      1          2           3          4
      2          3           1          4
      4          3           2          1
etc.

何时运行

`select * from xyz,`

我需要一个查询,而不是给我id,它实际上会从员工表中给我这些员工的姓名。

2 个答案:

答案 0 :(得分:1)

您需要多次加入staff

SELECT b.name AS current_staff,
       d.name AS next_staff,
       c.name AS hod_staff,
       e.name AS vp_staff
FROM   xyz a
       INNER JOIN staff b
               ON a.current_staff = b.id
       INNER JOIN staff c
               ON a.hod_staff = c.id
       INNER JOIN staff d
               ON a.next_staff = d.id
       INNER JOIN staff e
               ON a.vp_staff = e.id 

答案 1 :(得分:1)

如果我理解你的话,我想你可以在这里使用多次加入:

select a.name, b.name, c.name, d.name from xyz
        inner join staff as a on a.id = xyz.currentstaff
        inner join staff as b on b.id = xyz.nextstaff
        inner join staff as c on c.id = xyz.hodstaff
        inner join staff as d on d.id = xyz.vpstaff