doufen3563 2012-11-16 10:16
浏览 9
已采纳

基于外键复制几个mysql行

I want to copy several rows in a mysql table based on a foreign key, and asign the new rows a new foreign key ID. Assuming my table layout looks like this:

test
-----
table1_id int(11)
value varchar(20)

How do I accomplish this?

  • 写回答

1条回答 默认 最新

  • duanhuihui2705 2012-11-16 10:16
    关注

    Found out that the query would need to look like this:
    INSERT INTO test (table1_id, value) (SELECT '2', value FROM test WHERE table1_id=1)

    which would copy all rows with the foreign key ID '1' and assign the new rows with ID 2 instead. If the table contains more rows you could add them or change order in the SELECT part, like this

    ...(SELECT row1, '{$new_id}', value, another_row FROM...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到