比较 2 列,然后对 MySQL 中的另一列执行操作 [英] Compare 2 columns, then perform action on another column in MySQL
问题描述
我在 MySQL 中有一个名为 TableX
的表.TableX
中有 4 列.列是 ColumnCompare_Now
、ColumnCompare_Past
、ColumnNumber_Now
、ColumnNumber_Past
.
I have a table named TableX
in MySQL. There are 4 columns in TableX
. The columns are ColumnCompare_Now
, ColumnCompare_Past
, ColumnNumber_Now
, ColumnNumber_Past
.
我想写一个具有如下逻辑的MySQL语句;
I want to write a MySQL statement that has the following logic;
If ColumnCompare_Now == 'ActionNeeded' and ColumnCompare_Past == 'ActionNeeded',
then ColumnNumber_Now = `ColumnNumber_Now` + `ColumnNumber_Past`
MySQL 知识仅限于简单的 Select Where 语句.以上操作如何在MySQL中实现?同时,我会在等待 StackOverflow 其他成员的帮助的同时尝试自己解决这个问题.
MySQL knowledge is limited to simple Select Where statements. How can the above operation be implemented in MySQL? Meanwhile, I will try to figure this out myself while waiting for help from fellow members of StackOverflow.
推荐答案
我不完全确定我知道你想要完成什么.如果要选择包含此信息的新列,可以使用 case
语句:
I'm not completely sure I know what you're trying to accomplish. If you want to select a new column with this information, you can use a case
statement:
select
case when ColumnCompare_Now = 'ActionNeeded'
and ColumnCompare_Past = 'ActionNeeded'
then ColumnNumber_Now + ColumnNumber_Past
else ColumnNumber_Now
end as ColumnNumber_Now
from tablex
<小时>
如果你在 update
查询中需要这个:
update tablex
set ColumnNumber_Now = ColumnNumber_Now + ColumnNumber_Past
where ColumnCompare_Now = 'ActionNeeded'
and ColumnCompare_Past = 'ActionNeeded'
这篇关于比较 2 列,然后对 MySQL 中的另一列执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!