mysql 查詢資料後批量插入,實現id自增

2021-10-22 09:45:16 字數 1610 閱讀 6587

比如我有兩張表,role_auth(角色選單表)和role(角色表),表結構如下:

create table `role_auth` (

`id` varchar(32) character set utf8mb4 collate utf8mb4_general_ci not null,

`role_id` varchar(32) character set utf8mb4 collate utf8mb4_general_ci not null comment '角色id',

`menu_id` varchar(32) character set utf8mb4 collate utf8mb4_general_ci not null comment '選單id',

`is_deleted` tinyint(1) not null default '0' comment '是否刪除',

`create_time` datetime not null comment '建立時間',

`update_time` datetime not null comment '更新時間',

primary key (`id`) using btree,

unique key `uk_auth` (`role_id`,`menu_id`) using btree

) engine=innodb default charset=utf8mb4 collate=utf8mb4_general_ci row_format=dynamic comment='角色許可權表';

create table `role` (

`id` varchar(32) character set utf8mb4 collate utf8mb4_general_ci not null,

`name` varchar(16) character set utf8mb4 collate utf8mb4_general_ci not null comment '名稱',

`department_id` varchar(32) character set utf8mb4 collate utf8mb4_general_ci not null comment '部門id',

`is_deleted` tinyint(1) not null default '0' comment '是否刪除',

`create_time` datetime not null comment '建立時間',

`update_time` datetime not null comment '更新時間',

primary key (`id`) using btree

) engine=innodb default charset=utf8mb4 collate=utf8mb4_general_ci row_format=dynamic comment='角色表';

現在需要為所有角色增加乙個選單,選單id為27,sql語句怎麼寫呢?

set  @last_id := (select max(id) from role_auth);

insert into role_auth select @last_id:= @last_id+1 as id,id as role_id,'27',0,now(),now() from role;

mysql插入資料寫法 mysql 批量插入資料

mysql使用insert插入多條記錄,應該如何操作呢?下面就為您詳細介紹mysql使用insert插入多條記錄的實現方法,供您參考。看到這個標題也許大家會問,這有什麼好說的,呼叫多次insert語句不就可以插入多條記錄了嗎!但使用這種方法要增加伺服器的負荷,因為,執行每一次sql伺服器都要同樣對s...

mysql 批量插入 Mysql批量插入分析

前言最近發現幾個專案中都有批次插入資料庫的功能,每個專案中批次插入的寫法有一些差別,所以本文打算對mysql的批次插入做乙個詳細的分析。準備1.jdk1.7,mysql5.6.38 2.準備庫和表 測試與分析 下面準備幾種插入的方式來分析優劣 1.statement插入方式 準備資料,然後通過sta...

mysql批量插入資料 MySQL中批量插入資料

例1 方法一 sql語句操作 delimiter 以delimiter來標記用 表示儲存過程結束 create procedure pre 建立pre 儲存方法 begin declare i int 定義i變數 set i 2 while i 53 do insert into lineinfo ...