简体   繁体   English

MySQL:WHERE子句中的存储过程

[英]MySQL: Stored Procedure in WHERE clause

So I want to pass dynamic value of param to stored procedure from parent query however it is producing error, here is the query: 所以我想将参数的动态值从父查询传递给存储过程,但这会产生错误,这是查询:

SELECT *
FROM mytable
WHERE mytable.user_id IN (CALL getDownlines(mytable.user_id))

However when I run SP directly, it works fine: 但是,当我直接运行SP时,它可以正常工作:

CALL getDownlines(100)

Any ideas please ? 有什么想法吗?

Here is SP for reference, not sure how to convert it into function: 这是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 ;  

That is not possible to call a procedure from a select . select调用过程是不可能的。 Do it in 2 steps: 分两步完成:

  1. Call the procedure and store the result somewhere. 调用该过程并将结果存储在某处。
  2. Run the select against the result of the procedure. 针对过程结果运行选择。

Or turn your procedure into a function. 或者将您的过程变成一个函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM