简体   繁体   English

MySql更新和替换表字段相对于其他字段

[英]MySql Update and Replace a Table Field relative to other field

I want to replace a table field based on other table field condition, 我想根据其他表字段条件替换表字段,

How it now 现在怎么样

Table : question 
---------------------------
Category  | Subcat |
--------------------------
11           0
11           0
11           0
11           0
11           0
12           1
12           1
12           1
12           1
----------------------

I want to replace 0 with 5 where main category is 11 我想用主要类别为11的5替换0

I have tried below code 我试过下面的代码

UPDATE `question` SET `subcat` = REPLACE(`subcat`, '0', '5') 

This replace all 0 present in any main category not in based on particular 这将取代所有主要类别中不基于特定类别的所有0

You need a proper where condition for filter the rows you really need to change 您需要一个适当的条件来过滤您真正需要更改的行

UPDATE question 
SET subcat = 5 
where category =11 
and subcat  = 0 

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

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