Jump to content

Select and Insert


webmusic

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?

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.