简体   繁体   English

MySQL 更新有 2 个条件

[英]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在我的表中,我有一些具有相同“stock_id”的行,需要更新 2 个条件,但不起作用,只需更新值为 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但是我的尺码有 S - M - X 和相同的 stock_id

Eg: I need update qtde column where stock = 13 and size = S例如:我需要更新 q​​tde 列,其中 stock = 13 和 size = S

Add the other condition with AND in the WHERE clause.WHERE子句中使用AND添加其他条件。

$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.顺便说一句,如果$row是另一个查询的结果,您可以使用UPDATE + JOIN在一个查询中执行此操作,而不是循环。 See MySQL update table based on another tables value请参阅基于另一个表值的 MySQL 更新表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM