mysql 表结构如何设计可以很快的查出一个部门下所有子部门的员工【包含本部门】 10
展开全部
下面只定义了基本结构,其他的如索引,字符集等要酌情加上。
create table departments (
id int primary key,
name varchar(50) not null,
parent_id int
)
create table employee (
id int primary key,
department_id int not null,
name varchar(50) not null,
)
下面是一些伪代码
department = select * from departments where name = [department_name]
departments = select * from departments where parent_id = department.id
select * from employee where department_id in [departments.id + department.id]
create table departments (
id int primary key,
name varchar(50) not null,
parent_id int
)
create table employee (
id int primary key,
department_id int not null,
name varchar(50) not null,
)
下面是一些伪代码
department = select * from departments where name = [department_name]
departments = select * from departments where parent_id = department.id
select * from employee where department_id in [departments.id + department.id]
追问
这样设计相当于在查指定部门下所有员工的时候;用到了子查询。跟你说一下我们之前的设计吧:员工表中有一个部门的idStr字段如:*.1.2代表该员工是1部门的2子部门的直属员工。这样用like可以查出所有员工。但问题就是like用不到索引 ;要知道这是mysql。并且数据量是很大的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询