Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL选择查询_Php_Mysql - Fatal编程技术网

Php MySQL选择查询

Php MySQL选择查询,php,mysql,Php,Mysql,我在mysql中有一个巨大的员工数据,其属性作为父id,存储每个员工的主管,并在层次结构中定义。每个用户都在其他员工手下工作,并且是由4-5名成员组成的处理团队。我经常需要主管或下属树,我正在使用递归函数为其团队获取雇员。请给我推荐一个方法,这样我就不必每次需要员工数据时都调用递归函数。使用“视图或存储过程”是个好主意吗 谢谢 使用存储过程时,仍然需要递归。您只需将递归从PHP源代码移动到数据库 您可以使用来存储分层数据。这以较高的插入、删除和重新定位成本为代价消除了递归。基本上,您可以创建两个

我在mysql中有一个巨大的员工数据,其属性作为父id,存储每个员工的主管,并在层次结构中定义。每个用户都在其他员工手下工作,并且是由4-5名成员组成的处理团队。我经常需要主管或下属树,我正在使用递归函数为其团队获取雇员。请给我推荐一个方法,这样我就不必每次需要员工数据时都调用递归函数。使用“视图或存储过程”是个好主意吗


谢谢

使用存储过程时,仍然需要递归。您只需将递归从PHP源代码移动到数据库

您可以使用来存储分层数据。这以较高的插入、删除和重新定位成本为代价消除了递归。基本上,您可以创建两个附加字段
left
right
,其中
left
e1
从属于
e2
iff
e1.left>e2.left和&e1.right


这使得SELECT查询很难阅读,但效率很高。当所有其他操作都失败时,请执行此操作。

这是一个非递归存储过程实现,它显然只需要应用程序代码中的一个调用,而不需要n个调用(树的每一级调用一个)。建议远离嵌套集,坚持使用邻接列表实现-考虑connectbyOracle和CTE在sql server中-不要再说了

drop table if exists employees;
create table employees
(
emp_id smallint unsigned not null auto_increment primary key,
name varchar(255) not null,
boss_id smallint unsigned null,
key (boss_id)
)
engine = innodb;

insert into employees (name, boss_id) values
('f00',null), 
  ('ali later',1), 
  ('megan fox',1), 
      ('jessica alba',3), 
      ('eva longoria',3), 
         ('keira knightley',5), 
            ('liv tyler',6), 
            ('sophie marceau',6);


drop procedure if exists employees_hier;

delimiter #

create procedure employees_hier
(
in p_emp_id smallint unsigned
)
begin

declare v_done tinyint unsigned default(0);
declare v_dpth smallint unsigned default(0);

create temporary table hier(
 boss_id smallint unsigned, 
 emp_id smallint unsigned, 
 depth smallint unsigned
)engine = memory;

insert into hier select boss_id, emp_id, v_dpth from employees where emp_id = p_emp_id;

/* http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */

create temporary table emps engine=memory select * from hier;

while not v_done do

    if exists( select 1 from employees e inner join hier on e.boss_id = hier.emp_id and hier.depth = v_dpth) then

        insert into hier select e.boss_id, e.emp_id, v_dpth + 1 
            from employees e inner join emps on e.boss_id = emps.emp_id and emps.depth = v_dpth;

        set v_dpth = v_dpth + 1;            

        truncate table emps;
        insert into emps select * from hier where depth = v_dpth;

    else
        set v_done = 1;
    end if;

end while;

select 
 e.emp_id,
 e.name as emp_name,
 p.emp_id as boss_emp_id,
 p.name as boss_name,
 hier.depth
from 
 hier
inner join employees e on hier.emp_id = e.emp_id
left outer join employees p on hier.boss_id = p.emp_id;

drop temporary table if exists hier;
drop temporary table if exists emps;

end #

delimiter ;

-- call this sproc from your php

call employees_hier(1);

请查看以下链接:


为什么每次查询都会给您带来性能问题?如果只有几十条记录,听起来很难相信。但也许mySQL查询缓存值得一看(如果它还没有处于活动状态):你的意思是你需要为一名员工获取整个树,还是只获取下一个更高的或树部分?4-5个主键查找有什么问题?@Pekka我没有几十条记录。我的数据库中有数千名员工。非常感谢Oswald先生,请给我写一段详细的代码。谢谢先生,我从这个链接中得到了你的概念。很抱歉,我接受这个答案太晚了,因为我今天才知道it@Umar(以及任何阅读本文的人):本文的新地址:没有问题-一天(祈祷)mysql将具有CTE或connect by功能,这将使adj list与嵌套的set参数变得毫无意义。