我需要在一个表中插入数千条具有特定值的记录。只有一个条件,即两列具有动态值。
业务逻辑:我需要为所有客户的所有网站插入特定值。一个客户端可以有一个或多个网站。
如果我想编写静态SQL,那么我的SQL将有超过100k条记录要插入!
我希望插入到表website_preferences中的记录为
INSERT into website_preferences ( cid, website_id, key, value, is_preview, updated_by, updated_on, created_by, created_on, details )
VALUES ( DYNAMIC_CID, DYNAMIC_WEBSITE_ID, 'NEIGHBORHOOD_GOOGLE_PLACES', 'airport, gas_station, supermarket, gym', 123, NOW(), 123, NOW(), NULL )websites表包含cid和website_id以及其他元数据。
此查询将为我提供多达数十万条记录:
SELECT w.id,w.cid FROM websites w WHERE deleted_by IS NOT NULL如何从websites表中获取cid和id (网站id )并关联到insert语句?
发布于 2019-05-06 21:14:00
看起来你在找一个插件...选择:
INSERT into website_preferences (cid, website_id, key, value, is_preview, updated_by, updated_on, created_by, created_on, details)
SELECT w.id, w.cid, 'NEIGHBORHOOD_GOOGLE_PLACES', 'airport, gas_station, supermarket, gym', 123, NOW(), 123, NOW(), NULL
FROM websites w
WHERE deleted_by IS NOT NULL;发布于 2019-05-06 21:13:52
使用insert . . . select
insert into website_preferences ( cid, website_id, key, value, is_preview, updated_by, updated_on, created_by, created_on, details )
select ws.cid, ws.website_id, 'NEIGHBORHOOD_GOOGLE_PLACES', 'airport, gas_station, supermarket, gym', 123, NOW(), 123, NOW(), NULL
from websites ws
where ws.deleted_by is not null;发布于 2019-05-06 21:26:45
您可以尝试执行以下操作:
INSERT into website_preferences ( cid, website_id, key, value, is_preview, updated_by, updated_on, created_by, created_on, details )
SELECT w.id,w.cid,'NEIGHBORHOOD_GOOGLE_PLACES', 'airport, gas_station, supermarket, gym', 123, NOW(), 123, NOW(), NULL FROM websites w WHERE deleted_by IS NOT NULLhttps://stackoverflow.com/questions/56005970
复制相似问题