MySQL update a joined table

The multi-table UPDATE syntax in MySQL is different from Microsoft SQL Server. You don’t need to say which table(s) you’re updating, that’s implicit in your SET clause. UPDATE tableA a JOIN tableB b ON a.a_id = b.a_id JOIN tableC c ON b.b_id = c.b_id SET b.val = a.val+c.val WHERE a.val > 10 AND c.val > … Read more

Update MySQL with if condition

MySQL supports IF statement. UPDATE abbonamento SET punti = IF(tipo = ‘punti’, punti – 1, punti), bonus = IF(tipo <> ‘punti’, bonus – 1, bonus) WHERE id = 17 or you can also use CASE UPDATE abbonamento SET punti = CASE WHEN tipo = ‘punti’ THEN punti – 1 ELSE punti END, bonus = CASE … Read more

Bulk update mysql with where statement

The easiest solution in your case is to use ON DUPLICATE KEY UPDATE construction. It works really fast, and does the job in easy way. INSERT into `table` (id, fruit) VALUES (1, ‘apple’), (2, ‘orange’), (3, ‘peach’) ON DUPLICATE KEY UPDATE fruit = VALUES(fruit); or to use CASE construction UPDATE table SET column2 = (CASE … Read more