In MySQL’s SQL file, define variables and use variables
1. At this stage, there is a new requirement, and it is necessary to do some initial data work for the product when deploying the product. Prepare the corresponding SQL file in advance. Since multiple SQL statements need to be executed, and subsequent SQL statements depend on the primary key ID generated by the previous SQL statement. Therefore, you need to define variables and use variables in the SQL file.
2. The SQL statement in the SQL file is as follows:
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. View the table: operation_types, the value of the last recorded: id is 19. Look at the table: operation_products, the last record of its value is 19. Look at the table: operation_product_imgs, whose value is 95 for the last recorded: ID. as shown in Figure 1
4. Run the SQL file,[SQL]Query CreditBak_patch_3.2.0 start.[SQL]finished successfully. run successfully. as shown in Figure 2
5. Check the table: operation_types, the value of the last recorded: id is 20. Look at the table: OPERATION_PRODUCTS, the last record of its value is 20. Look at the table: operation_product_imgs, whose value is 96 for the last record of the id. in line with expectations. as shown in Figure 3
![运行 SQL 文件,[SQL] Query creditbak_patch_3.2.0 start。[SQL] Finished successfully。成功运行。](https://www.shuijingwanwq.com/wp-content/uploads/2020/08/2-10.png)
