Jump to content


Photo

Select and Insert


  • Please log in to reply
2 replies to this topic

#1 webmusic

webmusic
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 04 October 2003 - 02:30 AM

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?

#2 Dissonance

Dissonance
  • Members
  • PipPip
  • Member
  • 12 posts
  • LocationChicago, IL

Posted 04 October 2003 - 07:38 AM

[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.
Friends don't let friends vote Republican.

#3 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 04 October 2003 - 07:39 AM

Whats the version of mysql you use? I guess some older version doesnt support nested queries.Check that out!
Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users