Skip to content
Advertisement

MySQL Update with 2 conditions

In my table I have some rows with same ‘stock_id’, and need update with 2 conditions, but don’t work, just update all rows with value 1

$this->db->query('UPDATE stock_table SET size = IF(kit = 'n', 1, (1 * 10)) WHERE id = '.$row->stock_id);

But my size have S – M – X with same stock_id

Eg: I need update qtde column where stock = 13 and size = S

Advertisement

Answer

Add the other condition with AND in the WHERE clause.

$this->db->query('UPDATE stock_table SET qtde = IF(kit = 'n', 1, (1 * 10)) WHERE size = 'S' AND id = '.$row->stock_id);

BTW, if $row is the result of another query, you can use UPDATE + JOIN to do this in one query, instead of looping. See MySQL update table based on another tables value

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement