Page 1 of 1

how to copy a data from a table to anotehr table?

Posted: Sat Apr 05, 2003 11:52 pm
by omegahawk
well this is if im not mistaken to copy the whole data in a table to another table

INSERT into destination_table SELECT * from source_table;

but how about if i want to copy a data from table a to table b?

let say that we know the username and we want to copy his real name to another table.....

well i got this one but it wont work.

$sql = "UPDATE details,mn SET details.name=mn.name, where details.idnum=mn.idnum";
$result=mysql_query($sql) or die("Died inserting login info into db. Error returned if any: ".mysql_error());


it gives out an error

Died inserting login info into db. Error returned if any: You have an error in your SQL syntax near 'mn SET details.name=mn.name, where details.matric=mn.matric' at line 1


anyone?..

Posted: Fri Apr 11, 2003 7:12 am
by Coco
can you have a select in an insert query?
in mysql 3?
:?

Posted: Fri Apr 11, 2003 7:29 am
by twigletmac
Coco wrote:can you have a select in an insert query?
in mysql 3?
:?
Yes you can:
http://www.mysql.com/doc/en/INSERT_SELECT.html

Mac

Re: how to copy a data from a table to anotehr table?

Posted: Fri Apr 11, 2003 7:34 am
by twigletmac
omegahawk wrote:well i got this one but it wont work.

$sql = "UPDATE details,mn SET details.name=mn.name, where details.idnum=mn.idnum";
$result=mysql_query($sql) or die("Died inserting login info into db. Error returned if any: ".mysql_error());


it gives out an error

Died inserting login info into db. Error returned if any: You have an error in your SQL syntax near 'mn SET details.name=mn.name, where details.matric=mn.matric' at line 1
It probably isn't working because you're most likely using MySQL version 3 and that syntax is only supported by MySQL version 4.0.4 or above:
http://www.mysql.com/doc/en/UPDATE.html

You'd have to find another - most likely less simple - solution. Although it's normally not good practise to have identical information stored in different tables.

Mac