On 24/02/2010 05:46, Rob Gould wrote: > I'm not sure if I need to write a PHP for-loop to do this, or if it can all be done in one SQL statement? > > Basically, I want to copy all the barcodes from one table and put them into another table, but only if the barcode in the first table > 0, and only if the wineid's match from table to table. > > > Steps individually are something like this: > > 1) First, I get all the records from the "wine" table that have barcodes, like this: > > SELECT * FROM `wine` WHERE barcode2 > 0 > > The fields I need are "barcode2", and "wineid" > > > 2) Next, I need to match all the wineid's from this "wine" table with the wine id's from the "usersdata" table. Both fields in both tables are called "wineid". > > 3) Then, if the wineid's match, I need to copy the "barcode2" value from the wine table and put it into the field "custombarcode" in the "usersdata" table. > > > I'm tempted to write a PHP script which does a while-loop through all the records returned from the wine table and do the matching with the usersdata table, but I wouldn't be surprised if there's some sort of table-join-type query that can do all this in one step. > Hi, If your using MySQL (Other DBs may support this as well) you can insert multiple rows like this. INSERT INTO destination_table ( barcode2, wineID) SELECT barcode2, wineID FROM wine WHERE barcode2 > 0; (adjust to your spec) Check out the manual page here (MySQL 5.1): http://dev.mysql.com/doc/refman/5.1/en/insert-select.html You can jump to the manual for other versions from this page too. Build up your SELECT statement first and then add the INSERT line when you're happy its getting the right values. If you want to be really careful, insert into a temporary table first. Regards Ian -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php