如何在同时更新多个列的同时为多个列放置 where 子句?
我想同时更新 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 条件来检查每列?
任何建议表示赞赏..
I want to update more than 10 columns at the same time , and my problem is I want to put where clause for all these columns.
My code is:
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)
In the above statement I want to add condition as to update columns only when they are not 0.
for exmple,
UPDATE Customer AS C
SET name = a.name,
address= a.address,
...
..
WHERE a.name <> 0,
a.address <> 0,
a.telephone <> 0
....
...
Is it possible to put where condition to check each column?
Any suggestions are appreciated..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西(假设
name <> 0
是一个拼写错误,并且您的名称实际上是字符列)如果该列为空,则这实际上会将列更新为其当前值。
请注意,这不处理 NULL 值!如果您需要同等对待 NULL 和
''
,则需要使用coalesce(name, '')
代替。Something like this (assuming
name <> 0
is a typo and your names are really character columns)This essentially updates the column to it's current value if it's empty.
Note that this does not deal with NULL values! If you need to treat NULL and
''
identically you need to usecoalesce(name, '')
instead.