I want to batch insert into roles
table below SQL to initialize user's role information, I grant three roles
to every user
:
INSERT INTO `roles`
(`role_id`, `role_name`, `menu_id`, `role_updated_time`, `role_created_time`, `role_creator_user_id`, `role_sys_type`)
VALUES
(40, 'developer', '[1,2,3,4]', NULL, now(), 1027, 1),
(41, 'financial', '[1,2,3]', NULL, now(), 1027, 2),
(42, 'operation', '[1,2]', NULL, now(), 1027, 3);
I want to do this job in one SQL, so how to foreach
every usr_id
in users
table so that it insert in roles
table, the usr_id
is equivalent to role_creator_user_id
in roles
table, and the role_id
is auto_increment
.
I use this SQL but not work:
INSERT INTO `roles`
(`role_id`, `role_name`, `menu_id`, `role_updated_time`, `role_created_time`, `role_creator_user_id`, `role_sys_type`)
VALUES
(40, 'developer', '[1,2,3,4]', NULL, now(), (select usr_id from users), 1),
(41, 'financial', '[1,2,3]', NULL, now(), (select usr_id from users), 2),
(42, 'operation', '[1,2]', NULL, now(), (select usr_id from users), 3);
So I tried this, also not work:
INSERT INTO
roles
(role_id
,role_name
,menu_id
,role_updated_time
,role_created_time
,role_creator_user_id
,role_sys_type
) VALUES (select 100, 'developer','[1,2,3,4]',NULL,now(),usr_id,1 from users)