sql多表连接

下面演示一个三表连接查询。

用户表、部门表、用户部门关联表

select a.id as userId,dept_id,c.name as deptName
from user as a
left join user_dept as b
on a.id=b.user_id
left join department as c
on b.dept_id=c.id;

在这里插入图片描述

用户表、角色表、用户角色关联表

select a.id as userId,c.name as roleName
from user as a
left join user_role as b
on a.id=b.user_id
left join role as c
on b.role_id=c.id;

在这里插入图片描述

吴迪软件开发 小程序 ECMAScript 6 React.js
金牛区吴迪软件开发工作室博客