Sql 尝试检索不存在用户id的行
我试图编写一个只返回用户所在位置的查询 没有作为1的角色 表列为roleid、userid、usertype。userid为每个roleid都有一个条目。例如:Sql 尝试检索不存在用户id的行,sql,oracle,Sql,Oracle,我试图编写一个只返回用户所在位置的查询 没有作为1的角色 表列为roleid、userid、usertype。userid为每个roleid都有一个条目。例如: roleid userid 1 323 3 323 4 323 6 323 7 323 10 323 3 324 4 324
roleid userid
1 323
3 323
4 323
6 323
7 323
10 323
3 324
4 324
6 324
7 324
10 324
每次我尝试这个,它只是拿出一行,其中roleid是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;
我正在使用上面的方法,但这将返回没有roleid1的用户,但仍然返回具有其他角色的用户。我希望查询只返回没有1的用户。如果您有
用户
表,则可以执行以下操作:
select ID -- other relevant user fields here
from users
where ID not in (select userId from userRoles where roleId = 1)
select distinct userId
from userRoles
where userId not in (select userId from userRoles where roleId = 1)
如果没有单独的用户表(即userId
字段不是外键),您可以:
select ID -- other relevant user fields here
from users
where ID not in (select userId from userRoles where roleId = 1)
select distinct userId
from userRoles
where userId not in (select userId from userRoles where roleId = 1)
在这两种情况下,子查询从userRoles中选择userId,其中roleId=1
将为您提供一个具有1
角色的用户列表,您只需确保该用户的ID不在该列表中。这么简单吗
select distinct userid from table where UserID <> 1
从userid 1所在的表中选择不同的userid
您需要exists子句:
select userid from users
where not exists (select * from userroles where userid = users.userid and roleid = 1);
或者从所有用户的集合中减去roleid为1的用户集合:
select userid from users
minus
select userid from userroles where roleid = 1;
现在您已经编辑了请求,下面是更正的select语句:
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
);
或:
你能给我们看一些代码来更好地理解你需要什么吗?