【问题标题】:How to put where clause for multiple columns while updating multiple columns at the same time?如何在同时更新多个列的同时为多个列放置 where 子句?
【发布时间】:2011-09-02 08:34:07
【问题描述】:

我想同时更新超过 10 列,我的问题是我想为所有这些列添加 where 子句。

我的代码是:

UPDATE Customer AS c
                    SET 
                        name = a.name,
                        address= a.address,
                        telephone = a.telephone,
                        --
                        --
                        --


        FROM Customer a 
                      INNER JOIN 
                        ( SELECT casenumber
                               , max(currentDate) AS md 
                          FROM Customer 
                          GROUP BY casenumber
                        ) AS z 
                      ON  z.casenumber = a.casenumber
                      AND z.md = a.currentDate
                    WHERE (a.casenumber = c.casenumber)

在上面的语句中,我想添加条件以仅在列不为 0 时更新。

例如,

UPDATE Customer AS C
    SET name = a.name,
    address= a.address,
    ...

    ..
     WHERE a.name <> 0,
            a.address <> 0, 
            a.telephone <> 0
            ....
            ...

是否可以在 where 条件下检查每一列?

任何建议都表示赞赏..

【问题讨论】:

    标签: postgresql where-clause sql-update


    【解决方案1】:

    类似这样的东西(假设name &lt;&gt; 0 是一个错字并且你的名字确实是字符列)

     UPDATE customer AS c
         SET name = CASE WHEN name <> '' THEN a.name ELSE name END,
             address = CASE WHEN address <> '' THEN a.address ELSE address END
    

    如果列为空,这实质上会将列更新为其当前值。

    请注意,这不处理 NULL 值!如果您需要将 NULL 和 '' 视为相同,则需要使用 coalesce(name, '') 来代替。

    【讨论】:

      猜你喜欢
      • 2021-11-06
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 2012-04-11
      • 1970-01-01
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多