Forum Moderators: coopster & phranque

Message Too Old, No Replies

Copy one field from a table to another

based on 2 conditions

         

Night_Hawk

8:55 pm on Jun 1, 2003 (gmt 0)

10+ Year Member



Sorry if this issue has been discussed, I could not find any help on this specific problem.

I have 2 tables:
Table Subcat which has the following fields:
(catid, subcatcode, subcatname,...)

Table Products which has the following fields:
(productid, productname, catid, subcatname, subcatid (this field has nothing in it yet))

What I want to do is the following:
populate the field subcatid in the table products from the field subcatcode in the table Subcat based on 2 conditions,
the conditions are: when the catid from both tables are equal and when the subcatname fields are equal.

All I need is the SQL query. Any help from the SQL expert out there is appreciated. Thanks in advance.

brotherhood of LAN

6:04 am on Jun 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The INSERT ... SELECT [mysql.com] syntax is what you're after.

INSERT INTO products (subcatid) SELECT column FROM other_table WHERE field = criteria;

I'd make a copy of any table before getting to grips with these queries, it's easy to mistype the query and find yourself adding data to where you shoudnt....