user表(id,username,password),role表(id,rolename),user_role表(uid,rid),要求根据给定的role id集合,查询所有包含这些role的用户列表,跟数据库无关。sql咋写,求指点
user表(id,username,password),role表(id,rolename),user_role表(uid,rid),要求根据给定的role id集合,查询所有包含这些role的用户列表,跟数据库无关。sql咋写,求指点
select username from user c inner join (
select uid from user_role a inner join role b on a.rid = b.id where b.id = <input>
) d on c.id = d.uid
大概就是这样,欢迎大神指正,萌新一枚
先获取uid, 再取详细信息, 试试
你的要求是全包含,所以条件就得是a&b&c这种,sql里多是模糊查询,所以sql这样可行
你想要的是最后这个rs的结果吧
数据太多的话可以在where里进行下筛选
select user_name from user u where exists(
select uid, group_concat('rid') as rids from user_role ur where rid in(1,2,3) and u.u_id=ur.uid group by ur.uid having instr(rids,'1')>0 and instr(rids,'2')>0 and instr(rids,'3')>0 ....)