Copying MySQL data

All,

Another query has me stumped. I have tried a number of examples online, with no success.

I am trying to copy a few fields from one table to another. The examples I found online make it seem simple. Am I missing the obvious here?

I am trying this in it’s own PHP script, which is below. I thank you in advance for your help.

[php]<?php
error_reporting(E_ALL | E_NOTICE);
ini_set(‘display_errors’, ‘1’);
$con = mysqli_connect(“localhost”, “timw79_rfid”, “xpress13”, “timw79_rxrfid”);
$query = “INSERT TagHistory (TagID, ReaderID, AssignedMachine, AssignedRig, Lat, Long)
SELECT TagID, ReaderID, AssignedMachine, AssignedRig, Lat, Long
FROM TagHistoryprep”;

if (!mysqli_query($con,$query)) {
die('Error: ’ . mysqli_error($con));
}
?>[/php]

I am getting the following error:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Long) SELECT TagID, ReaderID, AssignedMachine, AssignedRig, Lat, Long FROM Tag' at line 1

I don’t see an “into”

INSERT INTO TagHistory 

I had read on various forums that “INTO” wasn’t needed for this command. However, I added it as you recommended.
[php]<?php
error_reporting(E_ALL | E_NOTICE);
ini_set(‘display_errors’, ‘1’);
$con = mysqli_connect(“localhost”, “timw79_rfid”, “xpress13”, “timw79_rxrfid”);
$query = “INSERT INTO TagHistory (TagID, ReaderID, AssignedMachine, AssignedRig, Lat, Long)
SELECT TagID, ReaderID, AssignedMachine, AssignedRig, Lat, Long
FROM TagHistoryprep”;

if (!mysqli_query($con,$query)) {
die('Error: ’ . mysqli_error($con));
}
?>[/php]
I get the same error.
[php]Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘Long) SELECT TagID, ReaderID, AssignedMachine, AssignedRig, Lat, Long FROM Tag’ at line 1[/php]

From: http://dev.mysql.com/doc/refman/5.1/en/insert-select.html

“INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;”

INSERT INTO database2.table1 (field1,field3,field9)
SELECT table2.field3,table2.field1,table2.field4
FROM table2;

TIPS:
To avoid ambiguous column reference problems when the SELECT and the INSERT refer to the same table, provide a unique alias for each table used in the SELECT part, and qualify column names in that part with the appropriate alias.

The order of the selected fields and the inserted fields must match, so you enter the correct data. Before doing this, use “describe database2.table1” and “describe database1.table2”, to make sure the new fields can hold the same kind of information."

Sponsor our Newsletter | Privacy Policy | Terms of Service