Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use update set from in sql update statement

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use update set from in the sql update sentence, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Execute the general sql update statement as update table_name set column_name=value where column_name1=value1 However, we sometimes need to update the fields used in one table into the data of a field in another table according to the relevant fields in two tables, that is, the primary key aid in table a has a foreign key bid related in table b, and the field in table b needs to be updated to the data of field c in table an according to the corresponding id in tables an and b. If updating one by one according to the above sql will be very time-consuming, so we need to use the statement usage of update set from.

There is a table in the database called student, as follows:

If from the real point of view, in a student table, then some people's age is entered incorrectly, then we need to correct these errors, in the case of a very large amount of data, the data is very time-consuming to modify, so we can use an excel table to core all the student information, and then import it into the database to become a temporary table.

The following is the temporary table we imported:

Where student_id is the generated guid, so we now need to update their age based on the name and execute the following sql statement:

Update student set age=tm.age from temporary tm where student.name=tm.name

After successful execution, you can see that our student table has been updated successfully:

Note: update, delete and other statements can not be undone after execution, return. Therefore, you have to be careful when operating. If you are worried about operational errors, you can first back up the database, or create two identical tables, execute them once, and then execute them in the formal table after success.

Thank you for reading this article carefully. I hope the article "how to use update set from in sql update sentence" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report