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

Native JS music player

This article example shares the specific code of ...

PHP related paths and modification methods in Ubuntu environment

PHP related paths in Ubuntu environment PHP path ...

6 interesting tips for setting CSS background images

Background-image is probably one of those CSS pro...

MySQL 5.7.17 installation and configuration method graphic tutorial under win7

I would like to share with you the graphic tutori...

MySQL online DDL tool gh-ost principle analysis

Table of contents 1. Introduction 1.1 Principle 1...

Introduction to the method attribute of the Form form in HTML

1 method is a property that specifies how data is ...

Detailed explanation of adding click event in echarts tooltip in Vue

Table of contents need Workaround 1. Set tooltip ...

CSS3 border effects

What is CSS# CSS (abbreviation of Cascading Style...

Do you know how many connections a Linux server can handle?

Preface First, let's see how to identify a TC...

JQuery implements hiding and displaying animation effects

This article shares the specific code of JQuery t...

How to find websites with SQL injection (must read)

Method 1: Use Google advanced search, for example...

How to generate Hive table creation statement comment script in MySQL metadata

Preface This article mainly introduces the releva...