webmusic Posted October 4, 2003 Share Posted October 4, 2003 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? Quote Link to comment https://forums.phpfreaks.com/topic/1111-select-and-insert/ Share on other sites More sharing options...
Dissonance Posted October 4, 2003 Share Posted October 4, 2003 [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. Quote Link to comment https://forums.phpfreaks.com/topic/1111-select-and-insert/#findComment-3774 Share on other sites More sharing options...
shivabharat Posted October 4, 2003 Share Posted October 4, 2003 Whats the version of mysql you use? I guess some older version doesnt support nested queries.Check that out! Quote Link to comment https://forums.phpfreaks.com/topic/1111-select-and-insert/#findComment-3775 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.