【问题标题】:sql to update table for multiple columns.?sql更新多列的表。?
【发布时间】:2020-04-04 21:27:55
【问题描述】:

我有一个 EMP 表,其中包含 id、name、job、salary、age、doj 等列。 这个 EMP 表有 10 条记录,ID 为 id1,id2....id10。

我需要一个 DML 语句来更新 4 个 ID(id2、id4、id6、id8)的名称。 这需要多个 where 条件。 如 - 名称=a 其中 id=id2 name=b 其中 id=id4。

请提出建议。

【问题讨论】:

  • 您使用哪种 DBMS?

标签: sql oracle sql-update


【解决方案1】:

您可以使用case 表达式。应该是这样的:

update emp
set name = case id
    when 'id2' then 'a'
    when 'id4' then 'b'
    when 'id6' then 'c'
    when 'id8' then 'd'
end
where id in ('id2', 'id4', 'id6', 'id8')

【讨论】:

  • @ManjunathM:id 列的数据类型是什么?
  • id 为 varchar2 名称为 long 类型
  • @ManjunathM:好的。查询不应引发此错误(此处不涉及数字)。请完全按照提供的方式运行查询。
猜你喜欢
  • 2013-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 2012-02-23
  • 2021-08-13
  • 2018-11-29
相关资源
最近更新 更多