MySQL - 存储过程

标签 mysql stored-procedures

我需要从存储过程中获取数据并将其显示在仪表板中。那么这件事的背景是。

对于存储过程中的每个选择查询,只会返回一条记录,我有大约 10 个这样的查询。除此之外,我还有一个查询,它将在同一存储过程中返回大约 5 行。

我希望这 5 条记录与同一存储过程中的其他查询一起显示为 1 行。

示例:

调用dashboard_proc()将会返回

Date       Amount    total_orders
1-1-2017   500.00    50

上面输出的一部分我想将另一个选择查询附加到同一存储过程(dashboard_proc)。将返回的另一个查询

Place      Total
Hyderabad    100
Bangalore    100
Chennai      200
Mumbai       100

我需要下面

Date      Amount  total_orders  Hyderabad  Bangalore  Chennai  Mumbai
1-1-2016  500.00     50            100       100        200      100

最佳答案

根据您的输入:

  1. 报告的第一部分只有一条记录,因此我使用了以下结构
 CREATE TABLE `ResultPart1` (
  `ReportDate` date NOT NULL,
  `Amount` float DEFAULT NULL,
  `total_orders` int(11) DEFAULT NULL
)

insert into ResultPart1 select '2017-01-01', 500.00 , 50;

<强>2。报告的第二部分将有不同的地点,这里使用的地点是单个词,没有使用关键字。

 CREATE TABLE `ResultPart2` (
       `Place` varchar(50) NOT NULL,
      `Total` int(11) DEFAULT NULL
    ) 
insert into  ResultPart2()
Select 'Hyderabad',    100 union 
Select 'Bangalore' ,   100 union 
Select 'Chennai'    ,  200 union 
Select 'Mumbai'      , 100;

<强>3。创建存储过程

CREATE PROCEDURE `new_procedureDashboard`()
BEGIN

Declare vPlaceCount int;
Declare vquery varchar(1000);
declare vPlace varchar(1000);
declare vPlaceTotal int;
-- select * from ResultPart1;
-- Select * from ResultPart2;

CREATE TEMPORARY TABLE IF NOT EXISTS Table_ResultPart2 
(
   Id int NOT NULL AUTO_INCREMENT,
   Place varchar(100),
   PlaceTotal int ,
   PRIMARY KEY (id)     
);
insert into Table_ResultPart2(Place,PlaceTotal)
Select distinct Place,Total from ResultPart2;

SET vPlaceCount=(Select count(*)from Table_ResultPart2);
WHILE vPlaceCount>0 DO
        SET vPlace=(Select Place from Table_ResultPart2 where Id=vPlaceCount);
        SET vPlaceTotal=(Select PlaceTotal from Table_ResultPart2 where Id=vPlaceCount);
        SET vquery=concat("", "alter table ResultPart1 add " ,vPlace ," int ;");
        Select vquery into @AddColumn;
        PREPARE stmt FROM  @AddColumn;
        -- Select @AddColumn;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
        SET vquery="";

       --   Select vPlace,vPlaceTotal;
        -- Update 
        SET SQL_SAFE_UPDATES = 0;
        SET vquery=concat("", "update ResultPart1 SET " ,vPlace ," = " ,vPlaceTotal ," where 1=1 ;");

        Select vquery into @UpdateColumn;
        -- select @UpdateColumn;
        PREPARE stmt1 FROM  @UpdateColumn;
        -- Select @AddColumn;
        EXECUTE stmt1;
        DEALLOCATE PREPARE stmt1;
        SET SQL_SAFE_UPDATES = 1;



        SET vPlaceCount=vPlaceCount-1;
        SET vPlace="" ;
END WHILE;
Select * from ResultPart1;
drop TEMPORARY TABLE IF EXISTS Table_ResultPart2;
END

说明: 使用循环在主结果表中添加了所需的列,并使用循环更新了新放置(添加)列的值。您需要在存储过程中进行更改,因为我使用了永久表(ResultPart1 和 ResultPart2)。

<强>4。结果 enter image description here

更改语句以删除列以防万一您想重新运行存储过程。

alter table ResultPart1 drop column Hyderabad;
alter table ResultPart1 drop column Bangalore;
alter table ResultPart1 drop column Chennai;
alter table ResultPart1 drop column Mumbai;

关于MySQL - 存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44408673/

相关文章:

php - Ajax 响应 HTML 响应

mysql - 可以使用 SQL 查询获取 "friendly"URL 吗?

Mysql存储过程: match the column rule to retrieve value

c# - 如何将 asp.net 下拉选择项作为输入传递给 c# 中的存储过程?

Mysql 过程语法更改为 Firebird 过程语法

mysql - 本地主机 mysql 访问被拒绝

MySQL 查询速度极慢

mysql - 我可以将带有 JOIN 的 SQL 语句分解为更小的查询或优化查询吗?

sql-server - 导入 SQL Server 中现有的存储过程

sql-server - 存储过程适用于 SQL Server 2000,但不适用于 SQL Server 2008R2