NotaGuruAtAll
NotaGuruAtAll

Reputation: 533

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?

Upvotes: 0

Views: 24

Answers (2)

Nelio Martins
Nelio Martins

Reputation: 11

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

Upvotes: 0

mukunda
mukunda

Reputation: 2995

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

Upvotes: 1

Related Questions