举个简单例子:
select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
1 7369 SMITH CLERK 7902 1980-12-17 1800 20
2 7499 ALLEN SALESMAN 7698 1981-2-20 1600 300 30
3 7521 WARD SALESMAN 7698 1981-2-22 1250 500 30
4 7566 JONES MANAGER 7839 1981-4-2 2975 20
5 7654 MARTIN SALESMAN 7698 1981-9-28 1250 1400 30
6 7698 BLAKE MANAGER 7839 1981-5-1 2850 30
7 7782 CLARK MANAGER 7839 1981-6-9 2450 10
8 7788 SCOTT ANALYST 7566 1987-4-19 3000 20
9 7839 KING PRESIDENT 1981-11-17 5000 10
10 7844 TURNER SALESMAN 7698 1981-9-8 1500 0 30
11 7876 ADAMS CLERK 7788 1987-5-23 1100 20
12 7900 JAMES CLERK 7698 1981-12-3 950 30
13 7902 FORD ANALYST 7566 1981-12-3 3000 20
14 7934 MILLER CLERK 7782 1982-1-23 1300 10
现在想查询某些人及其全部下属员工信息,oracle中可以这样查询:
select * from emp
connect by prior empno=mgr
start with empno in (7902,7788)
;
1 7788 SCOTT ANALYST 7566 1987-4-19 3000 20
2 7876 ADAMS CLERK 7788 1987-5-23 1100 20
3 7902 FORD ANALYST 7566 1981-12-3 3000 20
4 7369 SMITH CLERK 7902 1980-12-17 1800 20
在mysql中有什么号方法替换吗,本人在网上搜了比较多的是用函数:
CREATE FUNCTION getChildLst(rootId INT)
RETURNS varchar(1000) CHARSET utf8
BEGIN
DECLARE sTemp VARCHAR(1000);
DECLARE sTempChd VARCHAR(1000);
select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
1 7369 SMITH CLERK 7902 1980-12-17 1800 20
2 7499 ALLEN SALESMAN 7698 1981-2-20 1600 300 30
3 7521 WARD SALESMAN 7698 1981-2-22 1250 500 30
4 7566 JONES MANAGER 7839 1981-4-2 2975 20
5 7654 MARTIN SALESMAN 7698 1981-9-28 1250 1400 30
6 7698 BLAKE MANAGER 7839 1981-5-1 2850 30
7 7782 CLARK MANAGER 7839 1981-6-9 2450 10
8 7788 SCOTT ANALYST 7566 1987-4-19 3000 20
9 7839 KING PRESIDENT 1981-11-17 5000 10
10 7844 TURNER SALESMAN 7698 1981-9-8 1500 0 30
11 7876 ADAMS CLERK 7788 1987-5-23 1100 20
12 7900 JAMES CLERK 7698 1981-12-3 950 30
13 7902 FORD ANALYST 7566 1981-12-3 3000 20
14 7934 MILLER CLERK 7782 1982-1-23 1300 10
现在想查询某些人及其全部下属员工信息,oracle中可以这样查询:
select * from emp
connect by prior empno=mgr
start with empno in (7902,7788)
;
1 7788 SCOTT ANALYST 7566 1987-4-19 3000 20
2 7876 ADAMS CLERK 7788 1987-5-23 1100 20
3 7902 FORD ANALYST 7566 1981-12-3 3000 20
4 7369 SMITH CLERK 7902 1980-12-17 1800 20
在mysql中有什么号方法替换吗,本人在网上搜了比较多的是用函数:
CREATE FUNCTION getChildLst(rootId INT)
RETURNS varchar(1000) CHARSET utf8
BEGIN
DECLARE sTemp VARCHAR(1000);
DECLARE sTempChd VARCHAR(1000);
SET sTemp = “$”;
SET sTempChd =cast(rootId as CHAR);
WHILE sTempChd is not null DO
SET sTemp = concat(sTemp,”,”,sTempChd);
SELECT group_concat(empno) INTO sTempChd FROM emp where FIND_IN_SET(mgr,sTempChd)>0;
END WHILE;
RETURN substr(sTemp,3);
END
然后用
select * from emp
where FIND_IN_SET(empno,concat(getChildLst(7902),”,”,getChildLst(7788)))
;
本人在emp表empno字段建了索引,通过navicat for mysql 看上面的语句是全表扫描,没有走索引。
这种本人试过一些数据量比较大的表,查询就很慢。
讨教下大家能否有其他好的办法在mysql里实现上面的语句功能,另外还有个问题,就是这个函数一次只能返回一个父节点的全部子节点,假如本人要查询的参数是多个,例如一个子查询:
select * from emp
connect by prior empno=mgr
start with empno in ( select empno from emp where deptno=30)
;
该怎么实现这种呢?
解决方案
20
mysql没有这种函数,一般都是使用自定义函数编写,利用组合上层id,采用find_in_set函数实现层级查询