0


I am looking for an example query that will allow me to insert data from a table into multiple(2) tables. I read this post but it is for Microsoft SQL Server only and am not sure if this is how you do it in MySQL.

Thanks in advance.

1
  • Just doing it twice would probably be the easiest way Commented Oct 10, 2012 at 19:36

2 Answers 2

1

AFAIK mysql doesn't provide a way for inserting data into multiple tables in one command.

You have to use multiple commands, but you may lock table before using them to assure data integrity.

Sign up to request clarification or add additional context in comments.

2 Comments

Why would you need to lock the table?
Lets say he would be inserting 100k records and he will want to prevent user from adding records to t2 so the data will be consistent.
1

you will need to use multiple query/commands in order to insert into multiple tables.

$sql1 = mysql_query("SELECT * FROM tbl1");
$result = mysql_fetch_assoc($sql1);

$item1 = $result['item1'];
$item2 = $result['item2'];

$save_to_tbl2 = ("INSERT INTO tbl2(item1,item2)VALUES('".$item1."','"item2."')";
$save = mysql_query(save_to_tbl2);

$save_to_tbl3 = ("INSERT INTO tbl3(item1,item2)VALUES('".$item1."','"item2."')";
$save = mysql_query(save_to_tbl3);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.