比较 2 列,然后对 MySQL 中的另一列执行操作 [英] Compare 2 columns, then perform action on another column in MySQL

查看:43
本文介绍了比较 2 列,然后对 MySQL 中的另一列执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MySQL 中有一个名为 TableX 的表.TableX 中有 4 列.列是 ColumnCompare_NowColumnCompare_PastColumnNumber_NowColumnNumber_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆