How to use a field in one table to update a field in another table in MySQL

How to use a field in one table to update a field in another table in MySQL

1. Modify 1 column

update student s, city c
set s.city_name = c.name
where s.city_code = c.code;

2. Modify multiple columns

update a, b
set a.title=b.title, a.name=b.name
where a.id=b.id

• Subqueries

update student s set city_name = (select name from city where code = s.city_code);

Oracle query reports this error: single-row subquery returns more than one row How to solve it?

The database has multiple duplicate data according to your query conditions.

For example:

UPDATE "SYS_ROLE" A
SET A ."DEPT_ID" = (
  SELECT
    c."id"
  FROM
    "his_department_info" c
  WHERE
    c."dept_name" = A ."ROLE_NAME"

If the above SQL statement reports the error "single-row subquery returns more than one row", it means that the data of the two fields "dept_name" in table C and "ROLE_NAME" in table A are repeated.

Summarize

The above is what I introduced to you on how to use the fields in one table in MySQL to update the fields in another table. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Summary of Mysql update multi-table joint update method
  • An example of how to query data in MySQL and update it to another table based on conditions
  • Mysql updates certain fields of another table based on data from one table (sql statement)
  • How to query and update the same table in MySQL database at the same time
  • How to update another table in mysql
  • A solution to update the increase/decrease range and increase/decrease rate of the entire table with only one SQL statement

<<:  Example of implementing the Graphql interface in Vue

>>:  Detailed steps for Python script self-start and scheduled start under Linux

Recommend

JS calculates the probability of winning based on the prize weight

Table of contents 1. Example scenario 1.1. Set th...

How to get the real path of the current script in Linux

1. Get the real path of the current script: #!/bi...

Summary of principles for writing HTML pages for emails

Since HTML email is not an independent HOST page o...

How to add a certificate to docker

1. Upgrade process: sudo apt-get update Problems ...

How to use CSS to write different styles according to sub-elements

The effect we need to achieve: What is needed The...

Use PS to create an xhtml+css website homepage in two minutes

There are too many articles about xhtml+css websi...

Three ways to share component logic in React

Without further ado, these three methods are: ren...

IE8 Beta 1 has two areas that require your attention

<br />Related articles: Web skills: Multiple...

How to use CSS to pull down a small image to view a large image and information

Today I will talk about a CSS special effect of h...

CSS achieves footer "bottom absorption" effect

We often encounter this problem: how to use CSS t...

A brief analysis of how MySQL implements transaction isolation

Table of contents 1. Introduction 2. RC and RR is...

A brief discussion on the Linux kernel's support for floating-point operations

Currently, most CPUs support floating-point units...

Tutorial on installing VMWare15.5 under Linux

To install VMWare under Linux, you need to downlo...

JS interview question: Can forEach jump out of the loop?

When I was asked this question, I was ignorant an...