我需要插入一个包含一些值的表(例如:本例中为“NDA”)。如果我只需要插入一个值,这似乎很有效。我有大约十几个类似的值,我是否可以调整此查询以插入 { 'NDA', 'SDM', 'APM' } 值。很想知道是否可以在没有存储过程或复制粘贴相同语句并更改值的情况下完成。
INSERT IGNORE INTO customer_feature (customer_id, feature)
SELECT c.id, 'NDA' FROM
customer as c
where c.edition = 'FREE_TRIAL';
引用:mysql -> insert into tbl (select from another table) and some default values
最佳答案
这是你想要的吗?
INSERT IGNORE INTO customer_feature(customer_id, feature)
select c.id, f.feature
from customer c cross join
(select 'NDA' as feature union all select 'SDM' union all select 'APM'
) f
where c.edition = 'FREE_TRIAL';
关于MySQL 向表中插入一组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23303340/