用户包含多个角色
执行:
SELECT ur.user_id,u.name user_name,u.mail,ur.role_id,r.name role_name
FROM tb_user_role ur
LEFT JOIN tb_user u ON u.id=ur.user_id
LEFT JOIN tb_role r ON r.id=ur.role_id
WHERE u.is_delete=0
ORDER BY ur.user_id asc;
一个用户含有2个角色
使用GROUP_CONCAT返回带有来自一个组的连接的非NULL值的字符串结果,GROUP_CONCAT(expr)默认分隔符是“,” 。DISTINCT去重
执行:
SELECT ur.user_id,u.name user_name,u.mail,
GROUP_CONCAT(DISTINCT ur.role_id) role_id,
GROUP_CONCAT(DISTINCT r.name) role_name
FROM tb_user_role ur
LEFT JOIN tb_user u ON u.id=ur.user_id
LEFT JOIN tb_role r ON r.id=ur.role_id
WHERE u.is_delete=0
GROUP BY ur.user_id ORDER BY ur.user_id asc;