Jump to content

Archived

This topic is now archived and is closed to further replies.

webmusic

Select and Insert

Recommended Posts

Trying the following without success.

 

Need to pull several fields from 2 tables

and insert them into another table.

 

INSERT INTO $table3 SELECT field1,field2,field3 FROM $table1,$table2 WHERE $table1.id=$table2.member_id

 

field 1 is from table1, fields 2 and 3 are from table 2.

 

Any ideas?

Share this post


Link to post
Share on other sites

[php:1:6ae32ffd94]$result = mysql_query ( \"SELECT $table1.field1, $table2.field2, $table2.field3 FROM $table1 INNER JOIN $table2 ON $table1.id = $table2.member_id\" );

 

if ( !$result )

{

// Fail

}

else

{

$row = mysql_fetch_assoc ( $result );

$field1 = $row[\'field1\'];

$field2 = $row[\'field2\'];

$field3 = $row[\'field3\'];

 

$sql = \"INSERT INTO $table3 ( field1, field2, field3 ) VALUES ( \'$field1\', \'$field2\', \'$field3\' )\";

 

$result = mysql_query ( $sql );

 

if ( mysql_affected_rows() > 0 )

{

// Success

}

else

{

// Fail

}

}[/php:1:6ae32ffd94]

 

That should get you started.

Share this post


Link to post
Share on other sites

Whats the version of mysql you use? I guess some older version doesnt support nested queries.Check that out!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.