Radi
Radi

Reputation: 271

INSERT Data From One Table Into Multiple Tables in MySQL


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.

Upvotes: 0

Views: 1081

Answers (2)

Lian
Lian

Reputation: 1629

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);

Upvotes: 1

Vyktor
Vyktor

Reputation: 21007

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.

Upvotes: 1

Related Questions