I have a Stored Procedure in which I am running a query twice to get the desired output like this
CREATE PROCEDURE `GetSeries`(IN id INT)
BEGIN
select pid from //<----This Query is executed twice
(select pid from poststatus
where uid = id and pid > 10
order by pid desc limit 50
) T
UNION
select pid from postshare where uid = id and pid
between 10 and
(select pid from //<----Running The previous query again
(select pid from poststatus
where uid = id and pid > 10
order by pid desc limit 50
) T limit 1);
END
I want the query execute once and later on get the value from that query. How can I do so?
You can store the output of the first query into a temporary table.
Read the first pid
in the list into local variable and use the same in second query.
UNION
the temp table records with the second query for desired results.
Example:
CREATE PROCEDURE `GetSeries`( IN id INT )
BEGIN
declare _pid int;
create temporary table pid_list_50 as
select pid from poststatus
where uid = id and pid > 10
order by pid desc
limit 50;
select pid into _pid from pid_list_50 limit 1;
select * from pid_list_50
union
select pid from postshare
where uid = id and pid between 10 and _pid;
DROP TEMPORARY TABLE pid_list_50;
END