我在一个数据库表中添加了一个新列。我想尝试将该列填充为以前的记录。新记录将通过表格进行验证。这是一个例子。
| Quantity | Description | Price | Amount | Type |
----------------------------------------------------------
| 3 | Storage for Pallets | 3.99 | 11.97 | NULL |
| 3 | Handling for Pallets| 3.99 | 11.97 | NULL |
| 3 | Misc expense | 3.99 | 11.97 | NULL |
----------------------------------------------------------
我想根据说明中的关键字替换这些空值。例如,更新后的表如下所示。
| Quantity | Description | Price | Amount | Type |
--------------------------------------------------------------
| 3 | Storage for Pallets | 3.99 | 11.97 | Storage |
| 3 | Handling for Pallets| 3.99 | 11.97 | Handling |
| 3 | Misc expense | 3.99 | 11.97 | Misc |
--------------------------------------------------------------
关于更新语句的任何想法都可以做到这一点?
最佳答案
因为没有主键,所以您已将所有列值的值放入where子句中。或至少足够使您根据对数据的了解,更新没有机会超过2行。
这里以第一个更新为例:
update tbl
set "Type" = 'Storage'
where "Quantity" = 3
and "Description" = 'Storage for Pallets'
and "Price" = 3.99
and "Amount" = 11.97;
小提琴:
http://sqlfiddle.com/#!12/9fa64/1/0
如果有主键,那么您的WHERE子句将简单得多:
where pk_id_field = x
(因为您可以放心知道您将要更新所需的确切行,并且没有其他行具有该值)