1

我正在尝试编写一个查询,该查询仅返回他们没有角色为 1 的用户。

表列是roleid、userid、usertype。userid 对每个 roleid 都有一个条目。例如:

roleid     userid
  1           323    
  3           323
  4           323
  6           323
  7           323
  10          323    
  3           324
  4           324
  6           324
  7           324
  10          324

每次我尝试这个时,它只会删除角色 ID 为 1 的行,但我需要一个完整的用户,即 323 不显示,因为它有 1。

非常感谢您的帮助。

谢谢

编辑:

select * from usersroles where userid in (
select userid from uprofiles where networkid in 
(select network_id from network where usertype in (469,467,466,468))) and roleid !=1;

我正在使用上面的方法,但这会返回没有 roleid 1 的用户,但仍会返回具有其他角色的用户。我希望查询只返回没有 1 的用户。

4

4 回答 4

1

这么简单???

select distinct userid from table where UserID <> 1
于 2013-10-29T12:06:01.747 回答
1

您要么需要存在子句:

select userid from users 
where not exists (select * from userroles where userid = users.userid and roleid = 1);

或者你从所有用户的集合中减去角色 ID 为 1 的用户集合:

select userid from users
minus 
select userid from userroles where roleid = 1;
于 2013-10-29T12:07:52.963 回答
0

如果你有一张users桌子,你可以这样做:

select ID -- other relevant user fields here
from users
where ID not in (select userId from userRoles where roleId = 1)

如果您没有单独的用户表(即该userId字段不是外键),您可以这样做:

select distinct userId 
from userRoles
where userId not in (select userId from userRoles where roleId = 1)

在这两种情况下,子查询select userId from userRoles where roleId = 1都会为您提供一个具有该1角色的用户列表,您只需确保该用户的 ID 不在该列表中。

于 2013-10-29T12:04:14.610 回答
0

现在您已经编辑了您的请求,这里是您更正的选择语句:

select * from usersroles where userid in 
(
  select userid from uprofiles where networkid in 
  (
    select network_id from network where usertype in (469,467,466,468)
  )
)
and userid not in
(
  select userid from usersroles where roleid =1
);

或者:

select * from usersroles where userid in 
(
  select userid from uprofiles where networkid in 
  (
    select network_id from network where usertype in (469,467,466,468)
  )
  minus
  select userid from usersroles where roleid =1
);
于 2013-10-29T14:11:34.043 回答