【问题标题】:Group columns by the same value and update按相同的值对列进行分组并更新
【发布时间】:2020-06-06 01:13:45
【问题描述】:

给定一个表,其中包含几列(数字和索引很重要)和几行。

|number|index|line|
| 1234 | blue| 0  |
| 1234 | blue| 1  |
| 567  | blue| 0  |

我正在尝试将 index = blue 的行更新为 index = red,但列“number”和“index”不允许相同。我尝试将列分组在一起,如果列号 = 数字和索引 = 索引则返回错误语句

我怎样才能做到这一点?

预期结果:

|number|index|line|
|1234  |blue |0   |
|1234  |blue |1   |
|567   |red  |0   |

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    您可以将最小值更新为red

    update t join
           (select min(number) as min_number
            from t
            where index = 'blue'
           ) tt
           on t.number = tt.min_number and t.index = 'blue'
        set index = 'red';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 2021-02-09
      • 2016-02-10
      相关资源
      最近更新 更多