简体   繁体   English

创建MySQL临时表,然后从中分配CRUD

[英]Creating MySQL Temporary Table then Assign CRUD From It

i have created a stored procedure which have logic like this: 我创建了一个存储过程,其逻辑如下:

  1. getting the data result from 2 tables then create temporary table based on it (the data range is selected using two parameter start date and end date). 从2个表中获取数据结果,然后基于该表创建临时表(使用两个参数开始日期和结束日期选择数据范围)。
  2. select few columns from the temporary table then assign to another table (eg. X) 从临时表中选择几列,然后分配给另一个表(例如X)
  3. update the table (X). 更新表格(X)。

this is my stored procedure syntax: 这是我的存储过程语法:

  DELIMITER $$

  USE `sre`$$

  DROP PROCEDURE IF EXISTS `sp_checkPotentialProductf`$$

  CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_checkPotentialProductf`(IN sdate DATE, IN edate DATE)
BEGIN 
DECLARE sumtotal INT DEFAULT 0;
DECLARE first_margin DOUBLE;
DECLARE count_rows INT DEFAULT 0;
DECLARE startnum INT DEFAULT 0;

DECLARE start_mrg DOUBLE;
DECLARE adder DOUBLE;

/* assign table temporary and select the values */

CREATE TEMPORARY TABLE IF NOT EXISTS vShowTopSellingQuantitys AS
 SELECT
  `mt_pesanan`.`ms_langganan_idms_langganan`  AS `idms_langganan`,
  `mt_pesanan`.`ms_langganan_idms_kodebarang` AS `idms_kodebarang`,
  `md`.`nama`                         AS `nama`,
  `mt_pesanan`.`quantity`                     AS `quantity`,
  `mt_pesanan`.`harga`                        AS `harga`,
  `mt_pesanan`.`jumlah`                       AS `jumlah`,
  `mt_pesanan`.`discount`                     AS `discount`,
  `mt_pesanan`.`tgl_pesan`                    AS `tgl_pesan`,
  SUM(`mt_pesanan`.`quantity` )   AS `sum_product`
 FROM `mt_pesanan`  JOIN `ms_barang` `md`
 WHERE ((`mt_pesanan`.`tgl_pesan` <= edate) AND (`mt_pesanan`.`tgl_pesan` >= sdate)) 
       AND (md.`idms_kodebarang` = `mt_pesanan`.`ms_langganan_idms_kodebarang`)
     GROUP BY `mt_pesanan`.`ms_langganan_idms_kodebarang`
     ORDER BY SUM(`mt_pesanan`.`quantity` ) DESC;
/* end assign table temporary */

/* assign values to variables */
SELECT SUM(sum_product) INTO sumtotal FROM `vShowTopSellingQuantitys`;
SELECT COUNT(sum_product) INTO count_rows FROM `vShowTopSellingQuantitys`;
SELECT (sum_product/sumtotal)*100 INTO first_margin FROM `vShowTopSellingQuantitys` LIMIT 1; 

/* truncate database tmp*/ 
TRUNCATE tmp_produkpotensial;

/*creating algorithm to analyze the percentage quantity/total */
SET startnum = 1;

/*show result with percentage*/
SELECT `idms_kodebarang`, `nama`, sum_product , ((sum_product/sumtotal)*100) AS 'pecentage_margin' FROM `vShowTopSellingQuantitys`;

/*result execution from temporary table to analyze potential product */
INSERT INTO tmp_produkpotensial(idms_kodebarang,nama,percentage_margin)
SELECT `idms_kodebarang`, `nama`, ((sum_product/sumtotal)*100) AS 'percentage_margin' FROM `vShowTopSellingQuantitys`;

/* update the sum total of percentage */    
WHILE(count_rows >= startnum) DO
IF(startnum = 1) THEN
  SET start_mrg = 0;
  UPDATE tmp_produkpotensial SET tmp_produkpotensial.sum_margin = (percentage_margin + start_mrg) WHERE id_barang = startnum;
ELSE
  SELECT sum_margin INTO start_mrg FROM tmp_produkpotensial WHERE id_barang = (startnum - 1);
  UPDATE tmp_produkpotensial SET tmp_produkpotensial.sum_margin = (percentage_margin + start_mrg) WHERE id_barang = startnum;
END IF;

/* Assign Update to Update Sum Margin Column */
SET startnum = startnum + 1;
END WHILE;


 END$$

 DELIMITER ;

The stored procedure is successfully compiled, but when i call the stored procedure it always returns 0 rows. 存储过程已成功编译,但是当我调用存储过程时,它始终返回0行。

syntax to call the stored procedure: 调用存储过程的语法:

  CALL `sp_checkPotentialProductf`('2012-12-31','2012-01-01');

My Questions: 我的问题:

  1. can temporary table is used again to create further CRUD statement? 可以再次使用临时表来创建进一步的CRUD语句吗?
  2. is there any ideas where the fails goes on? 有什么想法继续失败吗?

thanks 谢谢

okay, i have discovered a mistake. 好吧,我发现了一个错误。 The method to call the stored procedure is 调用存储过程的方法是

CALL `sp_checkPotentialProductf`(2012-12-31,2012-01-01);

without the quotes. 没有引号。

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

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