简体   繁体   中英

SQL Insert from Select to same table but with a changed ID - MySQL

I need to copy a number of rows from a table that have the same id_shop value, then insert these rows back into the same table but with a different id_shop value. I'm not sure how to do the later part. I'm guessing that it will be a variation of the following.

  INSERT INTO `ps_hook_module`(`id_module`, `id_shop`, `id_hook`, `position`)
  SELECT `id_module`, `id_shop`, `id_hook`, `position` FROM `ps_hook_module` WHERE     
  `id_shop` = 1
INSERT INTO `ps_hook_module`(`id_module`, `id_shop`, `id_hook`, `position`)
SELECT `id_module`, 42, `id_hook`, `position` FROM `ps_hook_module` 
WHERE `id_shop` = 1

42 is a different id_shop value you wanted

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM