用于连接三个表的SQL查询

时间:2014-01-28 07:00:15

标签: sql join

我有三张桌子

用户

ID || login   || name     || crypted_password  || salt
 2 || teacher1|| teacher1 || #@@EDGCH          || SDWH@##

角色(主)

id||name
3 ||teacher

Role_users

id || role_id || user_id
2  ||  3      ||  2

现在从这三个表中我想获取其角色=老师的用户的crypted_pa​​ssword和salt值。

怎么做!!

3 个答案:

答案 0 :(得分:1)

您可以使用join语法根据匹配字段连接表:

SELECT crypted_password, salt
FROM   users
JOIN   role_users ON users.id = role_uses.user_id
JOIN   role ON role_users.role_id = role.id
WHEN   role.name = 'teacher'

答案 1 :(得分:0)

像这样加入

SELECT U.* 
FROM USER U 
JOIN Role_Users RU ON RU.UserID = U.ID
JOIN Role R ON R.ID = RU.Role_ID
WHERE <Your Conditions here>

答案 2 :(得分:0)

select crypted_password,salt
from Users U
inner join Role_users RU on RU.user_id = U.ID
inner join Role R on R.id = RU.role_id
where R.name='teacher'