添加MySQL表中不存在的行
表中有两列:添加MySQL表中不存在的行,mysql,Mysql,表中有两列:userid和roleid。每个用户至少应具有roleid4。目前有大约10000条记录,但不知怎么,很少的用户没有这个角色 可视示例: userid | roleid 1 1 1 4 2 1 2 4 3 1 <---------- userid 3 misses roleid 4! 4 1 4 4 userid | roleid 1
userid
和roleid
。每个用户至少应具有roleid
4。目前有大约10000条记录,但不知怎么,很少的用户没有这个角色
可视示例:
userid | roleid
1 1
1 4
2 1
2 4
3 1 <---------- userid 3 misses roleid 4!
4 1
4 4
userid | roleid
1 1
1 4
2 1
2 4
3.1是
having
子句中的sum(role=4)
统计每个拥有4
的用户的行数。=0
表示没有
注意:这为该表中的所有用户提供了一个4的角色id。可能存在完全没有角色的用户
如果需要,请使用users
表:
insert into userRoles(userid, roleid)
select u.userid, 4
from users u
where not exists (select 1 from userRoles ur where ur.userid = u.userid);
对
having
子句中的sum(role=4)
统计每个拥有4
的用户的行数。=0
表示没有
注意:这为该表中的所有用户提供了一个4的角色id。可能存在完全没有角色的用户
如果需要,请使用users
表:
insert into userRoles(userid, roleid)
select u.userid, 4
from users u
where not exists (select 1 from userRoles ur where ur.userid = u.userid);
您应该搜索roleID中没有4的用户
insert into yourTable ( userId, roledid)
select userid, 4
from yourTable
where roleid <>4
插入到您的表中(userId,roledid)
选择userid,4
从你的桌子上
roleid 4在哪里
您应该搜索roleID中没有4的用户
insert into yourTable ( userId, roledid)
select userid, 4
from yourTable
where roleid <>4
插入到您的表中(userId,roledid)
选择userid,4
从你的桌子上
roleid 4在哪里