此代码花费更多时间或由于数据超过2000而导致崩溃。请向我建议新的查询。
此查询适用于多级查询。 我的数据库每天都在增加。如果我有2M数据.... thannnn ... 帮帮我。
SELECT DISTINCT emp_id FROM emp WHERE (refferal in(select emp_id from emp where refferal in(select emp_id from emp where refferal in(select emp_id from emp where refferal ='2'))) or refferal in(select emp_id from emp where refferal in(select emp_id from emp where refferal ='2')) or refferal in(select emp_id from emp where refferal ='2'))
我正在使用php和mysqli。
答案 0 :(得分:0)
我的意思是下一个查询可以解决问题:
SELECT DISTINCT emp_id
FROM emp
LEFT JOIN emp as ref1 ON emp.referral = ref1.emp_id
LEFT JOIN emp as ref2 ON ref1.referral = ref2.emp_id
LEFT JOIN emp as ref3 ON ref2.referral = ref3.emp_id
WHERE
ref1.referral ='2' OR
ref2.referral ='2' OR
ref3.referral ='2';