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

在 MySQL 的 SQL 文件中,定义变量与使用变量

查看表:operation_types,其最后一条记录的:id 的值为 20。查看表:operation_products,其最后一条记录的:catalog 的值为 20。查看表:operation_product_imgs,其最后一条记录的:id 的值为 96。符合预期。
1、现阶段存在一个新需求,需要在部署产品时,针对产品做一些初始化数据的工作。预先准备好相应的 SQL 文件。由于需要执行多条 SQL 语句,且后续的 SQL 语句依赖于之前的 SQL 语句生成的主键 ID。因此,需要在 SQL 文件中,定义变量与使用变量。 2、SQL 文件中的 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、查看表:operation_types,其最后一条记录的:id 的值为 19。查看表:operation_products,其最后一条记录的:catalog 的值为 19。查看表:operation_product_imgs,其最后一条记录的:id 的值为 95。如图1
查看表:operation_types,其最后一条记录的:id 的值为 19。查看表:operation_products,其最后一条记录的:catalog 的值为 19。查看表:operation_product_imgs,其最后一条记录的:id 的值为 95。
图1
4、运行 SQL 文件,[SQL] Query creditbak_patch_3.2.0 start。[SQL] Finished successfully。成功运行。如图2
运行 SQL 文件,[SQL] Query creditbak_patch_3.2.0 start。[SQL] Finished successfully。成功运行。
图2
5、查看表:operation_types,其最后一条记录的:id 的值为 20。查看表:operation_products,其最后一条记录的:catalog 的值为 20。查看表:operation_product_imgs,其最后一条记录的:id 的值为 96。符合预期。如图3
查看表:operation_types,其最后一条记录的:id 的值为 20。查看表:operation_products,其最后一条记录的:catalog 的值为 20。查看表:operation_product_imgs,其最后一条记录的:id 的值为 96。符合预期。
图3

需要长期技术维护或远程问题排查?

我是拥有 15+ 年经验的 PHP / Go 后端工程师,长期关注已有系统维护、Bug 修复、性能优化、服务器排查、WordPress 网站维护和小功能迭代。

如果你的项目遇到以下情况,可以先从一次小问题排查开始合作:

  • ✅ PHP / Laravel / Yii2 老项目无人维护
  • ✅ Go / Gin 后端接口需要排查或优化
  • ✅ WordPress 网站访问慢、报错或插件冲突
  • ✅ Nginx / MySQL / Redis / Linux 服务器异常
  • ✅ CDN / Cloudflare / DNS / HTTPS 配置问题
  • ✅ 需要长期远程技术支持或兼职维护

更多介绍请查看:关于我 & 合作

微信:13980074657
邮箱:shuijingwanwq@gmail.com
Telegram:@shuijingwan
GitHub:https://github.com/shuijingwan

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理