简体   繁体   中英

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:

CALL getDownlines(100)

Any ideas please ?

Here is SP for reference, not sure how to convert it into function:

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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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