2

我正在尝试加入一些表以接收一些数据,但它没有按预期工作。这是我的查询:

SELECT USR.PK_User AS PKUser
    ,USR.NAME AS NAME
FROM [User] USR
LEFT JOIN aspnet_Users ASPU ON USR.NAME = ASPU.UserName
LEFT JOIN aspnet_UsersInRoles ASPUIR ON ASPU.UserId = ASPUIR.UserId
WHERE ASPUIR.RoleId = 'E8700479-902A-42F1-A500-4FA02CAC356C'

此查询没有返回任何内容。但如果我这样做:

SELECT * FROM [aspnet_UsersInRoles] WHERE RoleId = 'E8700479-902A-42F1-A500-4FA02CAC356C'

我得到 76 个选定的行。如果我这样做:

SELECT USR.PK_User AS PKUser
    ,USR.NAME AS NAME
FROM [User] USR
LEFT JOIN aspnet_Users ASPU ON USR.NAME = ASPU.UserName
LEFT JOIN aspnet_UsersInRoles ASPUIR ON ASPU.UserId = ASPUIR.UserId

我得到了返回的预期行。此外,如果我添加:

WHERE ASPUIR.RoleId IS NULL 

我得到行回来。

我究竟做错了什么?

4

2 回答 2

3

您需要ONnot the中的条件WHERE

否则,您只需将其有效地返回到内部连接。

SELECT USR.PK_User AS PKUser,
       USR.NAME    AS NAME
FROM   [User] USR
       LEFT JOIN aspnet_Users ASPU
         ON USR.NAME = ASPU.UserName
       LEFT JOIN aspnet_UsersInRoles ASPUIR
         ON ASPU.UserId = ASPUIR.UserId
            AND ASPUIR.RoleId = 'E8700479-902A-42F1-A500-4FA02CAC356C' 
于 2013-09-27T20:05:08.253 回答
1

尝试这个:-

SELECT USR.PK_User AS PKUser,
   USR.NAME    AS NAME
FROM   [User] USR
   LEFT JOIN aspnet_Users ASPU
     ON USR.NAME = ASPU.UserName
   LEFT JOIN aspnet_UsersInRoles ASPUIR
     ON ASPU.UserId = ASPUIR.UserId
        AND ASPUIR.RoleId = 'E8700479-902A-42F1-A500-4FA02CAC356C' 
于 2013-09-27T20:05:33.310 回答