Jump to content

Archived

This topic is now archived and is closed to further replies.

shaggycap

MySQL update statement

Recommended Posts

I am struggling with a SQL statement and hoped someone would be able to help :)

I have 2 tables:

customer_depots
contacts

Each table contain fields called 'customer_depot_id' and 'address_id'.

What I want to do is update the address_id in the contacts table, with the value of the address_id in the customer_depots table, only where the customer_id values match in each table, AND the address_id in the contacts table is equal to zero.

Does this make sense?

So, update address_id in contacts,
where address_id in contacts = address_id in customer_depots
and address id in contacts = zero.

This is because a number of address_id's are missing from the contacts table, and I want to replace them with the correspondingvalue from the other table.

I have got as far as a SELECT statement, which goes someway to explain things, but I need an UPDATE statement like I have explained above.

[code]SELECT cd.ADDRESS_ID FROM customer_depots cd, contacts con WHERE con.CUSTOMER_DEPOT_ID = cd.CUSTOMER_DEPOT_ID AND con.ADDRESS_ID = 0
[/code]
Hope someone can help :-*

thanks.






Share this post


Link to post
Share on other sites
[code]
UPDATE
contacts AS con
INNER JOIN
customer_depots AS cd
ON
con.CUSTOMER_DEPOT_ID = cd.CUSTOMER_DEPOT_ID
SET
con.address_id = cd.address_id
WHERE
con.address_id = 0
[/code]
http://dev.mysql.com/doc/refman/4.1/en/update.html

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.