用另一列中的值更新列
问题描述:
我有一个Sqlite表,其中ColumnA中具有相同值但ColumnB中具有不同值的行。下面的查询给了我几百行。用另一列中的值更新列
由具有计数ColumnA(*)> 1
我的要求是,对于具有相同ColumnA值的行,每当有一个在ColumnC在任何行中的值,填充在ColumnC值从MyTable的 组选择ColumnA所有行。以下是输入和输出。
有人可以帮忙吗?
+--------------------------+
| +------+-------+------+ |
+--------------------------+
| |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 |
| +------+-------+------+ |
+---------------------------------------------------+
答
这是不是你想要做什么?它使用相关的子查询来更新值:
update mytable
set columnc = (select max(columnC) from mytable t2 where t2.columnA = mytable.columnA)
where columnC is null;
这正是我想要的。没有意识到max(columnC)即使对于文本列也可以实现。非常感谢! – Aqua267 2014-09-30 19:18:41