MySQL:WHERE子句中的存储过程
因此,我想将param的动态值从父查询传递给存储过程,但它会产生错误,以下是查询:MySQL:WHERE子句中的存储过程,mysql,sql,Mysql,Sql,因此,我想将param的动态值从父查询传递给存储过程,但它会产生错误,以下是查询: SELECT * FROM mytable WHERE mytable.user_id IN (CALL getDownlines(mytable.user_id)) 但是,当我直接运行SP时,它工作正常: CALL getDownlines(100) 有什么想法吗 这里是SP供参考,不知道如何将其转换为函数: DELIMITER $ DROP PROCEDURE IF EXISTS getDownline
SELECT *
FROM mytable
WHERE mytable.user_id IN (CALL getDownlines(mytable.user_id))
但是,当我直接运行SP时,它工作正常:
CALL getDownlines(100)
有什么想法吗
这里是SP供参考,不知道如何将其转换为函数:
DELIMITER $
DROP PROCEDURE IF EXISTS getDownlines$
CREATE PROCEDURE getDownlines(in_id INT)
BEGIN
drop table if exists temp1;
drop table if exists temp2;
drop table if exists results;
create temporary table temp2 as (select id, upline_id from agents where upline_id = in_id);
create temporary table results as (select id, upline_id from temp2);
create temporary table temp1 (id int, upline_id int);
while (select count(*) from temp2) do
insert into temp1 (id, upline_id)
select a.id, upline_id
from agents a
where a.upline_id in (select id from temp2) ;
insert into results (id, upline_id)
select distinct id, upline_id
from temp1;
delete from temp2;
insert into temp2 (id, upline_id)
select distinct id, upline_id
from temp1;
delete from temp1;
end while;
select a.*
from results r
join agents a
on a.id = r.id;
drop table if exists temp1;
drop table if exists temp2;
drop table if exists results;
End $$
DELIMITER ;
无法从
选择调用过程。分两步进行:
调用该过程并将结果存储在某处
根据过程的结果运行select
或者将您的过程转换为函数。从选择无法调用过程。分两步进行:
调用该过程并将结果存储在某处
根据过程的结果运行select
或者将您的过程转换为函数。将其设置为函数,您可以调用它,将其设置为函数,您可以调用它,但我想从父查询传递动态参数,您能否提供一些示例,说明您的确切含义?THX您可以更改程序以生成所有用户ID的结果,或者您可以将程序代码直接放入select中。无法将程序代码直接放入select中,因为它包含drop、insert等命令,请参阅更新的问题谢谢不确定您的程序正在执行,但必须非常慢,你最好在你的过程的末尾加上一个select,以得到你想要的结果。我无法理解你的理论,无论如何,谢谢,我会自己弄明白的谢谢,但我想从父查询传递动态参数,你能提供一些例子来说明你的确切意思吗?THX您可以更改程序以生成所有用户ID的结果,或者您可以将程序代码直接放入select中。无法将程序代码直接放入select中,因为它包含drop、insert等命令,请参阅更新的问题谢谢不确定您的程序正在执行,但必须非常慢,你最好在程序的末尾加上一个选择,以得到你想要的结果。我无法理解你的理论,谢谢你,无论如何我会自己弄明白的