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

The difference between redundant and duplicate indexes in MySQL

MySQL allows you to create multiple indexes on a ...

Ubuntu installation graphics driver and cuda tutorial

Table of contents 1. Uninstall the original drive...

Detailed explanation of the difference between docker-compose ports and expose

There are two ways to expose container ports in d...

Linux service monitoring and operation and maintenance

Table of contents 1. Install the psutil package S...

MySQL high availability solution MMM (MySQL multi-master replication manager)

1. Introduction to MMM: MMM stands for Multi-Mast...

Teach you MySQL query optimization analysis tutorial step by step

Preface MySQL is a relational database with stron...

A detailed discussion of MySQL deadlock and logs

Recently, several data anomalies have occurred in...

WeChat applet realizes the nine-square grid effect

This article shares the specific code for the WeC...

Vue method to verify whether the username is available

This article example shares the specific code of ...

Implementation of textarea adaptive height solution in Vue

Table of contents Hidden Problems Solution to ada...

10 Deadly Semantic Mistakes in Web Typography

<br />This is from the content of Web front-...

Ubuntu 18.04 obtains root permissions and logs in as root user

Written in advance: In the following steps, you n...

JS realizes the scrolling effect of announcement online

This article shares the specific code of JS to ac...

Analysis of the Linux input subsystem framework principle

Input subsystem framework The linux input subsyst...