简体   繁体   中英

Mysql - Update One Table from another

I have two tables in a mysql database

car_table_one

and

car_table_two

The columns I am dealing with, or want to, are:

car_table_two: model_make_id model_name model_year model_weight_kg

car_table_one: make model year curb_weight

My goal is as follows, in plain English logic

if model_weight_kg = 0 copy curb_weight from car_table_one where model_make_id = make & model_name = model & model_year = year

What would such a query look like?

something like this...i think

UPDATE car_table_two t2 
INNER JOIN car_table_one t1 
ON t2.model_make_id = t1.model
AND t2.model_name = t1.model
AND t2.model_year = t1.year
SET t2.model_weight_kg = t1.curb_weight
WHERE t2.model_weight_kg = 0

A little correction to the mukunda SQL Sentence (ON comparation) and the query should work:

UPDATE car_table_two t2 
INNER JOIN car_table_one t1 
ON t2.model_make_id = t1.make
AND t2.model_name = t1.model
AND t2.model_year = t1.year
SET t2.model_weight_kg = t1.curb_weight
WHERE t2.model_weight_kg = 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM