简体   繁体   English

从存储过程 mysql 5.7 制作存储过程

[英]Make stored procedure from stored procedure mysql 5.7

I used this solution Mysql make a stored procedure from multiple stored procedures我使用了这个解决方案Mysql 从多个存储过程创建一个存储过程

I have 2 stored procedures which I want to make stored procedure with 2 stored procedures in 1 stored procedure.我有 2 个存储过程,我想在 1 个存储过程中使用 2 个存储过程创建存储过程。

This is procedure 1这是程序1

CREATE DEFINER=`brambang`@`%` PROCEDURE `buyer_statistic_monthly`(IN paramdatefrom datetime, IN paramdateto datetime)
BEGIN
CREATE TEMPORARY TABLE temp2 as SELECT 
count_PBR as PBR,
count_PUL as PUL,
count_PEX as PEX,
count_ID as pesanan
FROM 
(SELECT 
(select -- om.createdby, om.quantity, x1.count_
count(distinct om.createdby) as count_pbr
from (select count(xx.count_) as count_
from (select count(createdby) as count_
from order_match where
order_status_Id in (4, 5, 6, 8)
group by createdby having count(createdby) = 1) xx ) x1,
(select createdby from order_match where order_status_id in (4, 5, 6, 8)
 group by createdby having count(createdby) = 1) yy, order_match om where yy.createdby = om.createdby and
order_status_id in (4, 5, 6, 8) and
om.createdAt >= paramdatefrom and om.createdAt <= paramdateto
and NOT EXISTS
(select 1 from order_match om2 where om.createdby = om2.createdby
and order_status_id in (4, 5, 6, 8) and om2.createdAt < paramdatefrom)) count_PBR,
(SELECT count(distinct om.createdby) as count_PUL
from (select count(xx.count_) as count_
from (select count(createdby) as count_
from order_match where order_status_id in (4, 5, 6, 8)
group by createdby having count(createdby) > 1 ) xx ) x1,
(select createdby from order_match
where order_status_id in (4, 5, 6, 8)
group by createdby having count(createdby) > 1 ) yy,
order_match om where yy.createdby = om.createdby
and order_status_id in (4, 5, 6, 8) and om.createdAt >= paramdatefrom
and om.createdAt <= paramdateto
and EXISTS (select 1 from order_match om2 where om.createdby = om2.createdby
and order_status_id in (4, 5, 6, 8) and om2.createdAt <= paramdateto)) count_PUL,
(SELECT
count(distinct om.createdby) as count_PEX
from
order_match om
where om.order_status_id in (4,5,6,8)
and om.createdAt <= paramdateto
and om.createdAt >= paramdatefrom
and EXISTS (select 1 from order_match om2
where om.createdby = om2.createdby
and om2.createdAt < paramdatefrom and
om2.order_status_id in (4, 5, 6, 8))) count_PEX,
(SELECT count(id) from order_match where order_status_id in (4, 5, 6, 8) and createdAt between paramdatefrom
AND paramdateto) count_ID) a
;

END

This is procedure 2这是程序2

CREATE DEFINER=`brambang`@`%` PROCEDURE `TNP_PEMBELI_PERCENTAGE`(IN paramdatefrom datetime, paramdateto datetime)
BEGIN
CREATE TEMPORARY TABLE temp1 as 
SELECT 
    result_a / result_b * 100 AS percentage_PBR,
    100 - (result_a / result_b * 100) AS percentage_PEX
FROM
    (SELECT 
        (select -- om.createdby, om.quantity, x1.count_
                count(distinct om.createdby) as count_pbr
                from (select count(xx.count_) as count_
from (select count(createdby) as count_
from order_match where
order_status_Id in (4, 5, 6, 8)
group by createdby having count(createdby) = 1) xx ) x1,
(select createdby from order_match where order_status_id in (4, 5, 6, 8)
 group by createdby having count(createdby) = 1) yy, order_match om where yy.createdby = om.createdby and
order_status_id in (4, 5, 6, 8) and
om.createdAt >= paramdatefrom and om.createdAt <= paramdateto
and NOT EXISTS
(select 1 from order_match om2 where om.createdby = om2.createdby
and order_status_id in (4, 5, 6, 8) and om2.createdAt < paramdatefrom)) result_a,
            (SELECT 
                    COUNT(DISTINCT om.createdby) AS count
                FROM
                    (SELECT 
                    COUNT(xx.count_) AS count_
                FROM
                    (SELECT 
                    COUNT(createdby) AS count_
                FROM
                    order_match
                WHERE
                    order_status_Id IN (4 , 5, 6, 8)
                GROUP BY createdby) xx) x1, (SELECT 
                    createdby
                FROM
                    order_match
                GROUP BY createdby) yy, order_match om
                WHERE
                    yy.createdby = om.createdby
                        AND order_status_id IN (4 , 5, 6, 8)
                        AND om.createdAt >= paramdatefrom
                        AND om.createdAt <= paramdateto) result_b,
            (SELECT 
                    COUNT(DISTINCT om.createdby) AS count
                FROM
                    order_match om
                WHERE
                    om.order_status_id IN (4 , 5, 6, 8)
                        AND om.createdAt <= paramdateto
                        AND om.createdAt >= paramdatefrom
                        AND EXISTS( SELECT 
                            1
                        FROM
                            order_match om2
                        WHERE
                            om.createdby = om2.createdby
                                AND om2.createdAt < paramdatefrom
                                AND om2.order_status_id IN (4 , 5, 6, 8))) result_c
    ) a;
    END

based on 2 procedure above, I wrote this stored procedure so that 2 procedure can run in 1 stored procedure:基于上面的 2 个过程,我编写了这个存储过程,以便 2 个过程可以在 1 个存储过程中运行:

CREATE DEFINER=`brambang`@`%` PROCEDURE `TES`(IN paramdatefrom datetime, IN paramdateto datetime)
BEGIN
CALL buyer_statistic_monthly(paramdatefrom, paramdateto);
CALL TNP_PEMBELI_PERCENTAGE(paramdatefrom,paramdateto);
CREATE TEMPORARY TABLE master_temp AS (SELECT * FROM temp1) UNION ALL (SELECT * FROM temp2);
END

but I get an error但我收到一个错误

Error Code: 1222. The used SELECT statements have a different number of columns错误代码:1222。使用的 SELECT 语句具有不同的列数

I've tried with @vvvv4d solution, but still I get that error.我已经尝试过@vvvv4d 解决方案,但仍然出现该错误。

The Temp1 and Temp2 tables don't have exactly the same columns which is required to use union Temp1Temp2没有使用union所需的完全相同的

You need to make sure it's something like:你需要确保它是这样的:

select PEX, PBR from Temp1
union
select  percentage_PEX,  percentage_PBR from Temp2

If your going to use select * the tables have to have the same columns that your going to union together.如果您要使用select *表必须具有您要union在一起的相同列

You got你得到了

Error Code: 1222. The used SELECT statements have a different number of columns错误代码:1222。使用的 SELECT 语句具有不同的列数

Because Temp1 has columns Temp2 doesn't have.因为Temp1Temp2没有的列。

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

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