简体   繁体   English

更新查询,该查询将使用同一表的另一条记录中的值设置记录中的特定列

[英]Update query that will set specific columns in a record with values from another record of same table

look at this query; 查看此查询;

update user_data set old_status= 'SNNNNS',
                     user_group='15',
                     default_rate='DEFAULT',
                     entity_num='1001'
where user_name='Dasu';

I know I could write the query like that and get result, but I dont want to be writing the values. 我知道我可以这样写查询并得到结果,但是我不想写值。 These values are from another record with user_name 'sys' in the same table. 这些值来自同一表中具有user_name'sys'的另一条记录。 I want a query that will update these particular columns in 'Dasu' with values from 'sys'. 我想要一个查询,该查询将使用“ sys”中的值更新“ Dasu”中的这些特定列。

Any idea? 任何想法?

In Oracle, you can use merge with a self-join. 在Oracle中,可以将merge与自merge一起使用。 Alternatively, you can write correlated subqueries in the update : 另外,您可以在update编写相关的子查询:

update user_data
    set (old_status, user_group, default_rate, entity_num) = (select old_status, user_group, default_rate, entity_num from user_data where user_name = 'sys')
where user_name='Dasu';

You can nest a select in your update statement. 您可以将选择嵌套在更新语句中。 For Example: 例如:

UPDATE Dasu
SET old_status = (SELECT old_status FROM sys WHERE Id = 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用同一表中现有记录的值更新记录值 - How to Update record values with values from an existing record in same table 根据来自同一表的另一条记录的值更新值 - Update value based on value from another record of same table SQL查询-从同一表的1条记录更新许多记录 - SQL Query — update many records from 1 record in same table SQL 查询从表中获取记录的值并更新同一表中不同记录的获取值 - SQL query to get value from the table for a record and update the fetched value for different record in the same table SQL 查询与另一个表中的行集相关的最早记录 - SQL query for earliest record related to set of rows from another table 如何通过检查同一表中另一条记录中的值来更新记录 - How to update a record by checking a value in another record in same table 用同一表中的另一条记录的值更新一条记录的值 - Update value of one record with the value of another record in the same table 如果同一表中的值匹配,则更新记录 - Update record if matching values in same table 更新查询以设置另一个表中的值 - Update query to set values from another table 如何设置一个记录值等于同一个表中的另一个记录值 - How to Set a Record Value equal to Another Record Value in the Same Table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM