How to do a multiple insert using LAST_INSERT_ID() MySQL?

Armando Ávila Bueno

I have 3 tables:

  • customer
  • email
  • phone

Everyone of them with its own AUTO_INCREMENT id (id_customer, id_email, id_phone) and I have 2 more tables to relate them:

  • customer_email
  • customer_phone

These tables contain the others's tables ids as foreign keys.

How can I relate them in a single SQL statement using LAST_INSERT_ID()? Or what do you recommend?

Jordi Llull

You can store the results of LAST_INSERT_ID() using variables.

INSERT INTO email(...) VALUES (...);
SET @email_id = LAST_INSERT_ID();

INSERT INTO phone(...) VALUES (...);
SET @phone_id = LAST_INSERT_ID();

INSERT INTO customer_phone(id_email, id_phone) VALUES(@email_id, @phone_id);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to do a multiple insert using LAST_INSERT_ID() MySQL?

From Dev

MYSQL LAST_INSERT_ID not working as intended. How do I fix?

From Dev

MySQL LAST_INSERT_ID() - how does it works

From Dev

MySQL SQL_INSERT_ID() and LAST_INSERT_ID do not work

From Dev

MYSQL last_insert_id() and concurrency

From Dev

LAST_INSERT_ID after restarting MySQL

From Dev

MYSQL last_insert_id() and concurrency

From Dev

mysql - insert in two tables using autogenerate key from first insert table without using LAST_INSERT_ID

From Dev

how to select last_insert_id?

From Dev

How efficient is Last_insert_id?

From Dev

How to use LAST_INSERT_ID() when using uuid as a default value?

From Dev

Thread safety of MySQL's Select Last_Insert_ID

From Dev

MySQL : Stored procedure returns null for last_insert_id

From Dev

MySQL retrieve last_insert_id from stored procedure

From Dev

MySQL: Can't get LAST_INSERT_ID()

From Dev

MySQL Function with LAST_INSERT_ID() giving unexpected results

From Dev

Using select last_insert_id() from php not working

From Dev

how to get LAST_INSERT_ID via stored procedure in php

From Dev

How do I insert multiple records into a mysql database, using a combination of checkboxes and html select

From Dev

how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

From Dev

get first insert id for multiple insert using pdo in mysql

From Dev

How do I insert multiple images in rows from mysql database?

From Dev

How do I modify this MySQL INSERT Query for multiple rows?

From Dev

How do I insert Unique values in MySQL using using Java

From Dev

How to insert same data into multiple tables in mysql using triggers

From Dev

How do I insert multiple conditional comments using Jade?

From Dev

How do I insert the same line into multiple files using VIM

From Dev

How do you determine if an insert or update was successful using Java and MySQL?

From Dev

How do you INSERT ints and string VALUES into MySQL using MySQLdb?

Related Related

  1. 1

    How to do a multiple insert using LAST_INSERT_ID() MySQL?

  2. 2

    MYSQL LAST_INSERT_ID not working as intended. How do I fix?

  3. 3

    MySQL LAST_INSERT_ID() - how does it works

  4. 4

    MySQL SQL_INSERT_ID() and LAST_INSERT_ID do not work

  5. 5

    MYSQL last_insert_id() and concurrency

  6. 6

    LAST_INSERT_ID after restarting MySQL

  7. 7

    MYSQL last_insert_id() and concurrency

  8. 8

    mysql - insert in two tables using autogenerate key from first insert table without using LAST_INSERT_ID

  9. 9

    how to select last_insert_id?

  10. 10

    How efficient is Last_insert_id?

  11. 11

    How to use LAST_INSERT_ID() when using uuid as a default value?

  12. 12

    Thread safety of MySQL's Select Last_Insert_ID

  13. 13

    MySQL : Stored procedure returns null for last_insert_id

  14. 14

    MySQL retrieve last_insert_id from stored procedure

  15. 15

    MySQL: Can't get LAST_INSERT_ID()

  16. 16

    MySQL Function with LAST_INSERT_ID() giving unexpected results

  17. 17

    Using select last_insert_id() from php not working

  18. 18

    how to get LAST_INSERT_ID via stored procedure in php

  19. 19

    How do I insert multiple records into a mysql database, using a combination of checkboxes and html select

  20. 20

    how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

  21. 21

    get first insert id for multiple insert using pdo in mysql

  22. 22

    How do I insert multiple images in rows from mysql database?

  23. 23

    How do I modify this MySQL INSERT Query for multiple rows?

  24. 24

    How do I insert Unique values in MySQL using using Java

  25. 25

    How to insert same data into multiple tables in mysql using triggers

  26. 26

    How do I insert multiple conditional comments using Jade?

  27. 27

    How do I insert the same line into multiple files using VIM

  28. 28

    How do you determine if an insert or update was successful using Java and MySQL?

  29. 29

    How do you INSERT ints and string VALUES into MySQL using MySQLdb?

HotTag

Archive