如果在sql update query中也是如此

时间:2016-02-19 16:58:44

标签: mysql sql

我想在SQL查询中实现逻辑

update table 
    if (column1 = null) then set column1='123'
     else set column2 = '123'

2 个答案:

答案 0 :(得分:7)

使用条件逻辑,例如:

update t
    set column1 = coalesce(column1, '123'),
        column2 = (case when column1 is null then column2 else '123' end);

答案 1 :(得分:3)

UPDATE MyTable SET column1 = CASE 
                              WHEN column1 IS Null THEN '123'
                              ELSE column1
                             END,
                   column2 = CASE 
                              WHEN column1 IS Null THEN column2
                              ELSE '123'
                             END;