mysql - join two table to replace new value from 2th table if exist -


i have 2 table belew

table1:  cid -- name -- price -- mid 1            100   -- 1 2       b      110   -- 1 3       c      120   -- 1 4       d      120   -- 2  table2: id -- userid -- cid -- price 1     1         2      200 1     2         2      200 

i want data table 1 if there record in table2 refrenced table1 cid price of table2 replace price of table1.

for example userid 1 , mid 1 if data mentioned senario should in result;

1            100   2       b      200 3       c      120    

you can left join check null value in second table. if second price null use first table's price.

select t1.cid, t1.name  case when t2.price null  t1.price  else t2.price end price  table1 t1  left join table2 t2  on t1.cid = t2.cid  where t1.mid = 1  , (t2.userid = 1 or t2.userid null); 

try hopeful work.


Comments