MySQL:WHERE子句中的存储过程

时间:2017-02-06 08:55:36

标签: 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 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 ;  

1 个答案:

答案 0 :(得分:3)

无法从select调用过程。分2步完成:

  1. 调用程序并将结果存储在某处。
  2. 根据程序结果运行select。
  3. 或者将您的程序变成一个功能。