drhgzx4727 2009-10-02 11:31
浏览 101
已采纳

MYSQL插入..选择 - 带外键的多行和表?

I'm using an INSERT .. SELECT to 'duplicate' rows from a table (with a different foreign key).

This works fine, however it's inserting multiple rows and I now need to do another INSERT to insert multiple rows that are linked to each individual row in the previous SELECT... does that even make sense? Haha.

Basically there's a one to many relationship between table1 and table2. There's a one to one relationship between table1 and table3. I'm 'assigning' rows from table1(and their relevant linked table2 rows) to a new row on table3 (i have the mysql_insert_id for this one). I'm trying to do it with as few queries as possible.

I'll give an example,


You have a products table an options table and an option_items table. Each option has multiple option_items and each product can have multiple options applied to it.

Product1 has the option Colour applied to it, and Colour has the items Blue and Green, Product1 also has the Size option applied to it which has the items Small and Large.

I wish to duplicate the product with a new ID, which means duplicating the rows in both the options and option_items tables and re-creating their relating foreign keys.

I've created the new product and have the productid.

Now I need to duplicate the options get the id of the new option, duplicate the items and assign the ID of the newly created option to the items (maintaining the option to item relationships).

Any help would be muchly appreciated!

  • 写回答

1条回答 默认 最新

  • dongyi1159 2009-10-06 11:32
    关注

    Sorry for the crap question! I ended up just using a loop instead of complicated queries. Probably not the best solution, but by far the most straight forward :)

    Thanks for reading guys.

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

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上