从Worker表中获取带有id的Person-Name

时间:2018-04-24 16:29:51

标签: sql sql-server

我是sql服务器上的新手,我正在尝试做一些查询...

我有2个表格,其中W = worker:

表A:服务

Date | Time | Service_Name | ID_W1 | ID_W2 | ID_W3

表B:工人

ID_W | Name | Last Name

我的问题是如何设法制作这样的表格??

Date | Time | Service_Name | ID_W1 | Name_W1 | ID_W2 | Name_W2 | ID_W3 | Name_W3

非常感谢

1 个答案:

答案 0 :(得分:2)

这应该有效

SELECT s.date, 
       s.time, 
       s.service_name, 
       s.id_w1, 
       w1.NAME AS Name_W1, 
       s.id_w2, 
       w2.NAME AS Name_W1, 
       S.id_w3, 
       w3.NAME AS Name_W1 
FROM   services s 
       INNER JOIN workers w1 
               ON s.id_w1 = w1.id_w 
       INNER JOIN workers w2 
               ON s.id_w2 = w2.id_w 
       INNER JOIN workers w3 
               ON s.id_w3 = w3.id_w