Update two tables in MySQL using INNER JOIN and WHERE -


i don't think possible thought check if is, , if not ask efficient alternative.

objective: update table_1 , table_2 using 1 query inner join , where

currently have:

update table_1     join table_2 on table_1.user_id= table_2.user_id set      table_1.value = 9,     table_2.value_fan = 43      table_1.user_id = 1 , table_2.fan_id =1 

this correctly updates table_2 according where condition, entries updated in table_1 table_1.user_id = 1... ignoring condition table_2.fan_id = 1


edit sorry, should have been more clear, had typo above, corrected... below link sqlfiddle

http://sqlfiddle.com/#!2/58d7b/1

as hope can see table_2_winnings correctly updated 1 user, james, getting winnings (as has city_id =1 , group_id =1). users in table_1 in group_id = 1 updated. want james updated...

on table_1.city= table_2.city 

is join filter in query. since city can come multiple times in both tables, inner join acts cross join. see this fiddle . getting required columns added 1 more join filter

and table_1.user = table_2.user; 

so update query :

update table_1,table_2     set      table_1.table_1_winnings = 6,     table_2.table_2_winnings = 43      table_1.city = 1 , table_2.city_id =1 , table_1.city= table_2.city , table_1.user = table_2.user; 

fiddle


Comments