【问题标题】:SQL update query with IF ELSE使用 IF ELSE 的 SQL 更新查询
【发布时间】:2018-04-23 15:36:12
【问题描述】:

如何编写以下 SQL 查询 (SSMS)

我的表格有 2 列 - ColumnA 和 ColumnB

IF my_table.columnA < 15
  update my_table set my_table.ColumnB = 0
else IF my_table.columnA < 28
  update my_table set my_table.columnB = 1
else IF my_table.columnA < 43
  update my_table set my_table.columnB = 2
else IF my_table.columnA < 60
  update my_table set my_table.columnB = 3

感谢您的帮助!

【问题讨论】:

标签: sql if-statement sql-update


【解决方案1】:

你似乎想要一个case 表达式:

update mytable
    set columnB = (case when columnA < 15 then 0
                        when columnA < 28 then 1
                        when columnA < 43 then 2
                        when columnA < 60 then 3
                   end)
    where columnA < 60;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 2013-05-22
    • 2023-04-02
    • 2021-05-01
    • 2015-05-04
    • 1970-01-01
    相关资源
    最近更新 更多