大家好,是否可以重写查询:
select userid from User where userid not in(select userid from UserRole where roleid in(8));
作为加入?
问题是一个用户可能有多个角色
先感谢您。
mysql> desc User; +--------------------+-------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------------+-------------------+------+-----+---------+----------------+ | userId | int(11) | NO | PRI | NULL | auto_increment | | userName | varchar(50) | YES | | NULL | |
...以及其他用户相关列
mysql> desc UserRole; +--------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------+------+-----+---------+-------+ | userId | int(11) | NO | PRI | 0 | | | roleId | int(11) | NO | PRI | 0 | | +--------+---------+------+-----+---------+-------+
最佳答案
我还没有测试过这个,但我认为它有效。
select userID from user
left join UserRole
on user.userID = UserRole.userID
and UserRole.roleID = 8
where UserRole.roleID IS NULL
关于mysql - 将子查询(不在)重写为Join,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/412777/