没有不值得去解决的问题,也没有不值得去学习的技术!

Define and Use Variables in MySQL SQL Files

Check the table: operation_types, the id value of its last record is 20. Check the table: operation_products, the catalog value of its last record is 20. Check the table: operation_product_imgs, the id value of its last record is 96. This matches expectations.

1. Currently, there is a new requirement to initialize data for the product during deployment. I prepared the corresponding SQL file in advance. Because I need to execute multiple SQL statements, and subsequent SQL statements depend on the primary key IDs generated by previous SQL statements, I need to define and use variables in the SQL file.

2. The SQL statements in the SQL file are as follows:

SQL
INSERT INTO `operation_types`(`alias`, `name`) VALUES ('mrfl', '默认分类');
 
SET @typeId = (SELECT `id` FROM `operation_types` WHERE `alias` = 'mrfl' ORDER BY `id` DESC LIMIT 0,1);
 
INSERT INTO `operation_products`(`activity_name`, `product_name`, `quantity`, `status`, `create_by`, `exchange_quantity`, `create_time`, `modify_time`, `start_time`, `validity_date`, `credits1`, `credits2`, `credits3`, `exchange_times`, `thumb_img`, `banner_img`, `accept_way`, `accept_addr`, `link_man`, `link_phone`, `accept_time`, `accept_time_desc`, `member_nums`, `participation_nums`, `sort`, `end_time`, `type`, `market_price`, `order_start_time`, `order_end_time`, `rank`, `catalog`, `product_type`) VALUES ('默认商品', '商品简介', 1, 1, 0, 0, 1597650329, 1597650329, 1597593600, 0, 1, NULL, NULL, 1, '/upload/20200817/20203554555373.png', '/upload/20200817/20202146638315.png', 1, '中国(四川)自由贸易试验区成都高新区天府五街200号4号楼A区9楼', '华栖云', '13888888888', 2, '', 0, 0, 0, 2147483647, 2, 1, 1597593600, 2147483647, 1, @typeId, 1);
 
SET @productId = (SELECT `id` FROM `operation_products` WHERE `activity_name` = '默认商品' AND `catalog` = @typeId ORDER BY `id` DESC LIMIT 0,1);
 
INSERT INTO `operation_product_imgs`(`id`, `content`, `count`) VALUES (@productId, 'a:2:{i:0;s:35:\"/upload/20200817/20201164114857.png\";i:1;s:35:\"/upload/20200817/20202503127232.png\";}', 2);
 
INSERT INTO `operation_version`(`version`, `update_time`) VALUES ('3.2.0', NOW());

3. Check the table: operation_types, the id value of its last record is 19. Check the table: operation_products, the catalog value of its last record is 19. Check the table: operation_product_imgs, the id value of its last record is 95. As shown in Figure 1

Check the table: operation_types, the id value of its last record is 19. Check the table: operation_products, the catalog value of its last record is 19. Check the table: operation_product_imgs, the id value of its last record is 95.
Figure 1

4. Run the SQL file, [SQL] Query creditbak_patch_3.2.0 start. [SQL] Finished successfully. It ran successfully. As shown in Figure 2

Run the SQL file, [SQL] Query creditbak_patch_3.2.0 start. [SQL] Finished successfully. It ran successfully.
Figure 2

5. Check the table: operation_types, the id value of its last record is 20. Check the table: operation_products, the catalog value of its last record is 20. Check the table: operation_product_imgs, the id value of its last record is 96. This matches expectations. As shown in Figure 3

Check the table: operation_types, the id value of its last record is 20. Check the table: operation_products, the catalog value of its last record is 20. Check the table: operation_product_imgs, the id value of its last record is 96. This matches expectations.
Figure 3

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系