有没有办法将此插入合并到 select 查询中?

Is there a way to consolidate this insert into select query?

用例: 对于存储在 users table 中的某些特定测试 qa 用户,我想在另一个名为 user_limits 的 table 中更新或创建值,以便它们具有较高的限制值 1000

users table 定义:

create table if not exists users
(
    id int unsigned auto_increment
        primary key,
    email varchar(255) not null
)

user_limits定义:

create table if not exists user_limits
(
    user_id int unsigned not null,
    type_id smallint not null,
    remaining int not null,
    constraint user_limits_user_id_type_id_unique
        unique (user_id, type_id),
    constraint user_limits_user_id_foreign
        foreign key (user_id) references users (id)
);

我当前运行良好的查询是:

insert into user_limits
(user_id, type_id, remaining)
select id, 1, 1000
from users
where email like 'qa-%mydomain.com'
    ON DUPLICATE KEY UPDATE remaining = 1000;

insert into user_limits
(user_id, type_id, remaining)
select id, 2, 1000
from users
where email like 'qa-%mydomain.com'
    ON DUPLICATE KEY UPDATE remaining = 1000;

insert into user_limits
(user_id, type_id, remaining)
select id, 3, 1000
from users
where email like 'qa-%mydomain.com'
    ON DUPLICATE KEY UPDATE remaining = 1000;

insert into user_limits
(user_id, type_id, remaining)
select id, 4, 1000
from users
where email like 'qa-%mydomain.com'
    ON DUPLICATE KEY UPDATE remaining = 1000;

它是重复的,对 1 到 4 范围内的 type_id 值做同样的事情,稍后可能必须对 1 到 10 的类型做同样的事情。

我尝试插入多个值,但出现语法错误,因为我必须 select 然后插入。 任何 MySQL 专家都输入了如何编写更优雅、更短的查询?

我的 MySQL 版本是 8.0.23

类似

INSERT INTO user_limits (user_id, type_id, remaining)
WITH RECURSIVE cte AS ( SELECT 1 num
                        UNION ALL
                        SELECT num + 1 FROM cte WHERE num < @maximal_typeid_value )
SELECT users.id, cte.num, 1000
FROM users
CROSS JOIN cte
WHERE users.email LIKE 'qa-%mydomain.com'
ON DUPLICATE KEY UPDATE users.remaining = 1000;