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

图1

4、运行 SQL 文件,[SQL] Query creditbak_patch_3.2.0 start。[SQL] Finished successfully。成功运行。如图2

图2

5、查看表:operation_types,其最后一条记录的:id 的值为 20。查看表:operation_products,其最后一条记录的:catalog 的值为 20。查看表:operation_product_imgs,其最后一条记录的:id 的值为 96。符合预期。如图3

图3

永夜