如果满足其他字段的值,则完整设置字段的值 Table
Set Value for Field in complete Table if Value of other Field is met
我有一个奇怪的问题。
在我的数据库中,我有一个包含 348 个条目的 Table。有没有一种方法可以仅在满足特定条件时更改特定值?如果显示为 0,则将活动更改为否。
+---+--------+------+
|id |display |active|
+-------------------|
|1 |18 |yes |
|2 |28 |yes |
|3 |0 |yes |
+---+--------+------+
之后
+---+--------+------+
|id |display |active|
+-------------------|
|1 |18 |yes |
|2 |28 |yes |
|3 |0 |no |
+---+--------+------+
将列中的 active all 更改为 yes 或 no 我可以处理,但在这里我不知道这是否可能,或者我是否必须手动检查每个值。
有什么想法吗?
这听起来像是一个简单的更新语句:
update your_table set active = 'no' where display = 0;
如果您想根据 display
的值有条件地更新 active
的值,那么您可以使用像这样的 case 表达式:
update your_table set active = case when display = 0 then 'no' else 'yes' end;
或使用 MySQL IF 语句:
update your_table set active = if(display = 0, 'no', 'yes');
请注意,最后两个语句对所有行进行操作,因此如果您只需要更新一个子集,则应使用 where 子句。
我有一个奇怪的问题。
在我的数据库中,我有一个包含 348 个条目的 Table。有没有一种方法可以仅在满足特定条件时更改特定值?如果显示为 0,则将活动更改为否。
+---+--------+------+
|id |display |active|
+-------------------|
|1 |18 |yes |
|2 |28 |yes |
|3 |0 |yes |
+---+--------+------+
之后
+---+--------+------+
|id |display |active|
+-------------------|
|1 |18 |yes |
|2 |28 |yes |
|3 |0 |no |
+---+--------+------+
将列中的 active all 更改为 yes 或 no 我可以处理,但在这里我不知道这是否可能,或者我是否必须手动检查每个值。
有什么想法吗?
这听起来像是一个简单的更新语句:
update your_table set active = 'no' where display = 0;
如果您想根据 display
的值有条件地更新 active
的值,那么您可以使用像这样的 case 表达式:
update your_table set active = case when display = 0 then 'no' else 'yes' end;
或使用 MySQL IF 语句:
update your_table set active = if(display = 0, 'no', 'yes');
请注意,最后两个语句对所有行进行操作,因此如果您只需要更新一个子集,则应使用 where 子句。