[英]Make stored procedure from stored procedure mysql 5.7
我使用了这个解决方案Mysql 从多个存储过程创建一个存储过程
我有 2 个存储过程,我想在 1 个存储过程中使用 2 个存储过程创建存储过程。
这是程序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
这是程序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
基于上面的 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
但我收到一个错误
错误代码:1222。使用的 SELECT 语句具有不同的列数
我已经尝试过@vvvv4d 解决方案,但仍然出现该错误。
Temp1
和Temp2
表没有使用union
所需的完全相同的列
你需要确保它是这样的:
select PEX, PBR from Temp1
union
select percentage_PEX, percentage_PBR from Temp2
如果您要使用select *
表必须具有您要union
在一起的相同列。
你得到了
错误代码:1222。使用的 SELECT 语句具有不同的列数
因为Temp1
有Temp2
没有的列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.