douhui3760 2014-11-30 18:48
浏览 36

在相关的表mysql中插入

I have two MySQL tables (InnoDB). I have created a users table with "id_users" as the relationship key. Then I created a second table with a foreign key reference to "id_users" in the users table. How can I do a MySQL Insert into the second table?

Should I use JOIN?

  • 写回答

2条回答 默认 最新

  • doubiankang2845 2014-11-30 19:27
    关注

    Join is only for SELECT statement. What you want to do is just two simple inserts :

    1. Insert your row in users
    2. Get the last id from users
    3. Insert your row in the second table with the foreign key equal to the last id.

    In mysql, this can be :

    INSERT INTO user (id, username) VALUES (NULL, 'john');
    INSERT INTO group (id, id_users) VALUES (NULL, LAST_INSERT_ID())
    

    LAST_INSERT_ID works for the entire database... be carefull using it.

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法