Update Database With Selected Data in mySql

I have to work with some mysql query to update some field in a mysql table. I have two tables. First one is 'Table_A' and other one is "Table_B". What I had to do is update 'customer_package_id' filed in "Table_A" Table according to their customer id (cus_id).


This is my Table_A
This is my Table_B

What I have to do is Update costomer_package_id field of Table_A with corresponding customer_package_id in Table_B. See below Figure.

There are several hundreds of statements in both tables. Before update Table_A. So I executed select query before update.

Select Query.
SELECT 
`a`.`cus_id`,`b`.`customer_package_id`, `a`.`priority` 
FROM 
`Table_A` `a` 
INNER JOIN 
`Table_B` `b` 
ON 
`a`.`cus_id`=`b`.`cus_id`

Then I have executed update query.
UPDATE 
`TABLE_A` `a` 
JOIN 
`TABLE_B` `b` 
ON 
`a.cus_id = b.cus_id`  
SET 
`a.customer_package_id = b.customer_package_id` 

Following Update Query also worked for me.
UPDATE 
`table_A` `a`
JOIN 
`table_B` `b`
ON 
   `a.cus_id` = `b.cus_id`
SET 
`a.customer_package_id` = `b.customer_package_id`

It got several seconds to execute the update query. My Job Done !

0 comments:

Post a Comment

Ask anything about this Tutorial.