将数据插入新列,同时依赖于另一列的数据
Insert data to a new column, while depending on data from another column
我在 table:
中添加了一个新专栏
Column Name: "Status"
DataType = varchar
在同一个 table 中,我有一个名为 Bin
的列,数据类型为 INT
,它包含值:0 和 1(1000 行)。
我想做的是,当“Bin”=1 时,在“Status”中插入“Success”一词,当“Bin”=0 时,在“Status”中插入“Failure”。
知道如何使用 SQL 语句执行此操作吗?
使用 Case
表达式你可以更新你的 table.
Update table_name
Set Status = Case Bin
WHEN 1 THEN 'Success'
WHEN 0 THEN 'Failure'
END
您可以为此目的添加一个计算列,而不是添加一个列来存储完全确定的数据:
alter table T add [Status] as case Bin when 1 then 'Success' else 'Failure' end
我在 table:
中添加了一个新专栏Column Name: "Status"
DataType = varchar
在同一个 table 中,我有一个名为 Bin
的列,数据类型为 INT
,它包含值:0 和 1(1000 行)。
我想做的是,当“Bin”=1 时,在“Status”中插入“Success”一词,当“Bin”=0 时,在“Status”中插入“Failure”。
知道如何使用 SQL 语句执行此操作吗?
使用 Case
表达式你可以更新你的 table.
Update table_name
Set Status = Case Bin
WHEN 1 THEN 'Success'
WHEN 0 THEN 'Failure'
END
您可以为此目的添加一个计算列,而不是添加一个列来存储完全确定的数据:
alter table T add [Status] as case Bin when 1 then 'Success' else 'Failure' end