更新 ch05_SQL高级处理.md

This commit is contained in:
mba1398 2025-04-07 17:26:43 +08:00 committed by GitHub
parent 74a47dc35f
commit bbe3bc7151
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 9 deletions

View File

@ -281,16 +281,23 @@ show tables;
- 插入数据 - 插入数据
```SQL ```SQL
-- 使用 SQL 客户端进行创建 -- 使用 SQL 客户端进行创建
CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_product_test`() -- 按照不同维度来汇总每日销售数据,分别插入相应的统计表
CREATE PROCEDURE `P_SALES_STATISTICS`(IN SDATE VARCHAR(20))
BEGIN BEGIN
declare i int; DELETE FROM shop.sales_statistics_1 WHERE sdate = SDATE;
set i=1; INSERT INTO shop.sales_statistics_1
while i<9 do SELECT sdate, product_id, product_name, SUM(sales_quantity) AS sales_quantity, SUM(sales_amount) AS sales_amount
set @pcid = CONCAT('000', i); FROM shop.sales_record
PREPARE stmt FROM 'INSERT INTO product_test() SELECT * FROM shop.product where product_id= ?'; GROUP BY sdate, product_id, product_name;
EXECUTE stmt USING @pcid;
set i=i+1; DELETE FROM shop.sales_statistics_2 WHERE sdate = SDATE;
end while; INSERT INTO shop.sales_statistics_2
SELECT a.sdate, b.product_type, SUM(a.sales_quantity) AS sales_quantity, SUM(a.sales_amount) AS sales_amount
FROM shop.sales_record a
LEFT JOIN shop.product b
ON a.product_id = b.product_id
GROUP BY a.sdate, b.product_type;
END END
``` ```