case-then多列sql语句

gkl3eglg  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(371)

那么,如何使用case设置两列的值呢?我有下一个剧本:

UPDATE [dbo].[operation_data]
SET block_status = CASE
WHEN sd.status = 'X' THEN 'F'
WHEN sd.status != 'X' and od.block_status = 'F' THEN NULL
ELSE block_status
END
FROM operation_data od
JOIN sales_data sd on sd.operation_data_id = od.operation_data_id
GO

但我想这样做

WHEN sd.status = 'X' THEN 'F' AND main_comment = 'Expired'

“main_comment”是操作数据处的列。我知道case只能返回一列。有什么好办法吗?

tuwxkamq

tuwxkamq1#

您还必须对其他列重复此操作:

UPDATE [dbo].[operation_data]
SET block_status = CASE
WHEN sd.status = 'X' THEN 'F'
WHEN sd.status != 'X' and od.block_status = 'F' THEN NULL
ELSE block_status
END
, main_comment = CASE
WHEN sd.status = 'X' THEN 'expired'
ELSE main_comment
END
FROM operation_data od
JOIN sales_data sd on sd.operation_data_id = od.operation_data_id

相关问题