Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update Table1 with a value from Table2 1

Status
Not open for further replies.

pastorandy

IS-IT--Management
Nov 2, 2006
84
GB
Hiv

I have two tables and all I want to do is update the cust
with a value from the territory


The tables are related and have a foreign key.

Here's what I have thus far:
Code:
UPDATE cust c, territory t
SET c.t_office_id = t.t_office_id 
WHERE c.t_office_id = t.t_office_id
AND c.postcode1 = t.postcode1

I just keep getting the error:
Code:
You have an error in your SQL syntax near 'c, territory t SET c.t_office_id = t.t_office_id WHERE c.t_office_id = t.t_offic' at line 1

Any ideas?
 
I think the post_town needs to have single quotes around it because the field values are text and numbers. (e.g AB10)
 
Hi feherke

Many thanks for all of your help with this!

Just managed to get the table updated which will save me a lot of work.

Thanks
 
Hi

Just one more question!

how would I write a query to just update one column in the cust table with the number 1 and then increment the numbers for all of the customer rows.up to 5,000.

 
Hi

Assuming that the post_town field is unique and not null. Otherwise use the actual primary key instead.
Code:
<?php
$c=mysql_connect("localhost","myUser", "myPassword");
mysql_select_db("myDatabase");
$r=mysql_query("select post_town from territory order by post_town");
while ($a=mysql_fetch_array($r))
  print("update cust set [green][i]theColumn[/i][/green]=".(++$i)." where post_town=".mysql_real_escape_string($a[0]).";\n");
mysql_close($c);
?>

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top