选择并更新结果SQL查询

时间:2021-12-04 07:46:06

Table name emp contains the below details

表名emp包含以下详细信息

|ID     |EmpID  |Date         |
|6248   |56     |15/5/18 9:10 | 
|8743   |87     |14/5/18 10:00|
|7656   |78     |15/5/18 13:56|

In the same table emp, I got three new ID's

在同一个表emp中,我有三个新的ID

ID
5643
5678
8954

I want to update the table for the new ids with the same EmpID and Date.

我想更新具有相同EmpID和日期的新ID的表。

Like this

|ID     |EmpID  |Date         |
|5643   |56     |15/5/18 9:10 |
|5678   |87     |14/5/18 10:00|
|8954   |78     |15/5/18 13:56|

Is it possible to do that in a single query to select and update the details?

是否可以在单个查询中执行此操作以选择和更新详细信息?

I need to update more than 2000+ entries and I will be creating a script do the same but before that, I need to find the query to do the same.

我需要更新2000多个条目,我将创建一个脚本做同样的事情,但在此之前,我需要找到相同的查询。

Any help much appreciated.

任何帮助非常感谢。

1 个解决方案

#1


1  

One way of doing this is via conditional updating.

一种方法是通过条件更新。

UPDATE table
   SET column1 = CASE id 
                      WHEN '1' THEN 'abc' 
                      WHEN '2' THEN 'bcd' 
                      ELSE column1
                      END
WHERE id IN('1', '2'); 

The query aboves updates column1 for the rows with id 1 and 2 to abc and bcd.

上面的查询将id1和2的行的column1更新为abc和bcd。

#1


1  

One way of doing this is via conditional updating.

一种方法是通过条件更新。

UPDATE table
   SET column1 = CASE id 
                      WHEN '1' THEN 'abc' 
                      WHEN '2' THEN 'bcd' 
                      ELSE column1
                      END
WHERE id IN('1', '2'); 

The query aboves updates column1 for the rows with id 1 and 2 to abc and bcd.

上面的查询将id1和2的行的column1更新为abc和bcd。