首页 文章

使用其他列中的值更新列

提问于
浏览
0

我有一个Sqlite表,其中ColumnA中的行具有相同的值,但ColumnB中的值不同 . 以下查询给了我几百行 .

从具有Count(*)> 1的ColumnA的MyTable Group中选择ColumnA

我的要求是对于具有相同ColumnA值的行,只要ColumnC中的任何行中有值,就在所有行中填充ColumnC值 . 以下是输入和输出 .

有人可以帮忙吗?

+--------------------------+
| +------+-------+------+  |
+--------------------------+
| |ColumnA|ColumnB|ColumnC |
| +------+-------+------+  |
| |C2     |Val1   |        |
| +------+-------+------+  |
| |C2     |Val2   |P       |
| +------+-------+------+  |
| |B2     |Val3   |Q       |
| +------+-------+------+  |
| |B2     |Val4   |        |
| +------+-------+------+  |
+--------------------------+

输出:

+---------------------------------------------------+
|              +------+-------+------+              |
+---------------------------------------------------+
| |ColumnA|ColumnB|ColumnC                          |
| +------+-------+------+                           |
| |C2     |Val1   |P        //add P to this row     |
| +------+-------+------+                           |
| |C2     |Val2   |P                                |
|                                                   |
| +------+-------+------+                           |
| |B2     |Val3   |Q                                |
| +------+-------+------+                           |
| |B2     |Val4   |Q          //add Q to this row   |
| +------+-------+------+                           |
+---------------------------------------------------+

1 回答

  • 2

    这样做你想要的吗?它使用相关子查询来更新值:

    update mytable
        set columnc = (select max(columnC) from mytable t2 where t2.columnA = mytable.columnA)
        where columnC is null;
    

相关问题