我在数据库表中有3列id, location_A, location_B
id location_A location_B
1 ROOM 1 ROOM 2
2 ROOM 2 ROOM 3
3 ROOM 5 MEETING ROOM
现在我想添加一个新的列名长度,如下面的值将在查询中添加
case when LOCATION_A='ROOM 1' AND LOCATION_B='ROOM 2' THEN 6
when LOCATION_A='ROOM 2' AND LOCATION_B='ROOM 3' THEN 8
等等
id location_A location_B length
1 ROOM 1 ROOM 2 6
2 ROOM 2 ROOM 3 8
3 ROOM 5 MEETING ROOM 50
答案 0 :(得分:1)
您可以在CASE WHEN
查询中使用UPDATE
语句:
UPDATE MyTable
SET length =
(case
when LOCATION_A='ROOM 1' AND LOCATION_B='ROOM 2' THEN 6
when LOCATION_A='ROOM 2' AND LOCATION_B='ROOM 3' THEN 8
else 50
end)
<强> SQLFiddle Demo 强>
更新:
以上SQL的Microsoft Access版本:
UPDATE MyTable
SET length =
switch(
LOCATION_A='ROOM 1' AND LOCATION_B='ROOM 2', 6
, LOCATION_A='ROOM 2' AND LOCATION_B='ROOM 3', 8
, true, 50)
答案 1 :(得分:0)
如果长度计算的逻辑实际上基于 location_A 和 location_B 中的实际数据,您可以执行以下操作:
创建视图,只需将长度添加为计算列
在表格中添加一个新列,并使用您的计算更新它