Case Then几列SQL语句

时间:2018-06-20 08:04:58

标签: mysql sql set case

如何使用Case-Then设置两个列的值? 我有下一个脚本:

  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”是operation_data中的列。 据我所知,CASE只能返回单列。是一些好的解决方案吗?

1 个答案:

答案 0 :(得分:2)

您也必须在另一列中重复此操作:

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