2014-09-24 28 views
-2

表批准人:內加入的3個表

No Userid 
1 3 
2 7 

表用戶:

No UserID RoleID 
1 3  1 
2 4  1 
3 5  2 
4 7  3 

表角色

RoleID Name 
1  ABC 
2  BCD 
3  CDE 

我想在表審批等來選擇用戶的角色名:

Userid Name 
3  ABC 
7  CDE 
+3

到目前爲止,您嘗試了什麼以及您的解決方案存在什麼問題? – Pred 2014-09-24 11:06:52

回答

1

我不是100%肯定,爲什麼approver.no是對用戶和審批表....我要去假設用戶id是獨一無二的在這兩種情況下,如果.....那的情況下這應該工作:

select 
    u.userid, 
    r.name 
from 
    Approver as a 
    inner join [Users] as u on a.userId = u.UserId 
    inner join [Roles] as r on u.roleId = r.roleId 

如果情況並非如此,你所需要的approver.no user.UserI d組合是唯一比下面應該工作:http://sqlfiddle.com/#!3/0daa9/4

注意到第二個查詢返回對一個結果:

select 
    u.userid, 
    r.name 
from 
    Approver as a 
    inner join [Users] as u on a.userId = u.UserId 
    and a.No = u.No 
    inner join [Roles] as r on u.roleId = r.roleId 

這兩個至於結果集的擔憂可以在這裏找到之間的差異提供的數據

0
SELECT U.UserID, 
     Name 
FROM Approver A 
     JOIN Users U 
      ON A.UserId = U.UserId 
     JOIN Roles R 
      ON R.RoleId = U.RoleId 

SQL FIDDLE DEMO

1
select 
    a.Userid, 
    r.Name 
from 
    Approver a 
    join Users u on a.no = u.no 
    join Roles r on u.RoleID = r.RoleID 
0
SELECT Approver.UserId, Name from Approver INNER JOIN Users 
on Approver.No=Users.No 
    INNER JOIN Roles 
on Users.RoleID=Roles.RoleID