An example of how to query data in MySQL and update it to another table based on conditions

An example of how to query data in MySQL and update it to another table based on conditions

This article uses an example to describe how MySQL can query data and update it to another table based on conditions. Share with you for your reference, the details are as follows:

The original database has 3 tables

  • travel_way: travel route table, storing detailed information of the route
  • traveltag: route tag table, storing route destination and other information
  • tagrelation: tag correspondence table, storing the correspondence between routes and destinations

Because of the change in business logic, we now need to merge them into one table and insert the destination information in traveltag into travel_way.

First, get the destinations corresponding to all routes, group them by route ID, merge the destinations into one line, and separate them with commas.

Copy the code as follows:
SELECT travel_way.id,GROUP_CONCAT(traveltag.content) FROM travel_way LEFT JOIN tagrelation on travel_way.id = tagrelation.travel_id LEFT JOIN traveltag ON tagrelation.tag_id = traveltag.id GROUP BY travel_way.id

First store the found data in a newly created table mid

Copy the code as follows:
INSERT into mid (travelway_id,destination) SELECT travel_way.id,GROUP_CONCAT(traveltag.content) FROM travel_way LEFT JOIN tagrelation on travel_way.id = tagrelation.travel_id LEFT JOIN traveltag ON tagrelation.tag_id = traveltag.id GROUP BY travel_way.id

Then update the data of the mid table to travel_way. Because it is an update, you cannot use the insert into select from statement.

Copy the code as follows:
update travel_way,mid set travel_way.destination = mid.destination where travel_way.id = mid.travelway_id

Successfully imported the destinations into the travel_way table as a comma-delimited string

Let's talk about the methods used, group_concat

group_concat( [DISTINCT] Field to be connected [Order BY sort field ASC/DESC] [Separator 'separator'] ), this function can combine the same rows

select * from goods;
+------+------+
|id| price|
+------+------+
|1 | 10|
|1 | 20|
|1 | 20|
|2 | 20|
|3 | 200 |
|3 | 500 |
+------+------+
6 rows in set (0.00 sec)

Group by id and print the values ​​of the price field on the same line, separated by commas (default)

select id, group_concat(price) from goods group by id;
+------+--------------------+
| id| group_concat(price) |
+------+--------------------+
|1 | 10,20,20|
|2 | 20 |
|3 | 200,500|
+------+--------------------+
3 rows in set (0.00 sec)

Group by id, print the price field in one line without duplication, separated by commas

select id,group_concat(distinct price) from goods group by id;
+------+-----------------------------+
| id| group_concat(distinct price) |
+------+-----------------------------+
|1 | 10,20|
|2 | 20 |
|3 | 200,500 |
+------+-----------------------------+
3 rows in set (0.00 sec)

Group by id, print the value of the price field in one line, separated by commas, and sorted in descending order of price

select id,group_concat(price order by price desc) from goods group by id;
+------+---------------------------------------+
| id| group_concat(price order by price desc) |
+------+---------------------------------------+
|1 | 20,20,10 |
|2 | 20|
|3 | 500,200|
+------+---------------------------------------+
3 rows in set (0.00 sec)

insert into select from insert into select from a table.

INSERT INTO db1_name(field1,field2) SELECT field1,field2 FROM db2_name

The target db2 must exist. Let's test it. There are two tables with the following structure:

select * from insert_one;
+----+--------+-----+-----+
| id | name | age | sex |
+----+--------+-----+-----+
| 1 | Tian Xiaosi | 25 | |
| 2 | Liu Daniu | 26 | |
| 3 | Zheng Dachui | 28 | |
| 4 | Hu Ergou | 30 | |
+----+--------+-----+-----+
4 rows in set

 
select * from insert_sex;
+----+-----+
| id | sex |
+----+-----+
| 1 | 1 |
| 2 | 2 |
| 3 | 1 |
| 4 | 2 |
+----+-----+
4 rows in set

Find gender data from Table 2 and insert it into Table 1

into insert_one(sex) select sex from insert_sex;
Query OK, 4 rows affected
select * from insert_one;
+----+--------+-----+-----+
| id | name | age | sex |
+----+--------+-----+-----+
| 1 | Tian Xiaosi | 25 | |
| 2 | Liu Daniu | 26 | |
| 3 | Zheng Dachui | 28 | |
| 4 | Hu Ergou | 30 | |
| 5 | | | 1 |
| 6 | | | 2 |
| 7 | | | 1 |
| 8 | | | 2 |
+----+--------+-----+-----+
8 rows in set

The result is embarrassing. I want to update the sex field of this table instead of inserting new data. Then this command is only applicable to importing data into an empty table. So in the actual need above, I created a new table mid and used update to transfer and update the data.

Copy the code as follows:
UPDATE tb1,tb2 SET tb1.address=tb2.address WHERE tb1.name=tb2.name

According to the matching conditions, replace (update) the data in Table 1 with the data in Table 2. Table 1 and Table 2 must be related.

update insert_one,insert_sex set insert_one.sex = insert_sex.sex where insert_one.id = insert_sex.id;
Query OK, 4 rows affected
select * from insert_one;
+----+--------+-----+-----+
| id | name | age | sex |
+----+--------+-----+-----+
| 1 | Tian Xiaosi | 25 | 1 |
| 2 | Liu Daniu | 26 | 2 |
| 3 | Zheng Dachui | 28 | 1 |
| 4 | Hu Ergou | 30 | 2 |
| 5 | | | 1 |
| 6 | | | 2 |
| 7 | | | 1 |
| 8 | | | 2 |
+----+--------+-----+-----+
8 rows in set

The data is successfully updated into the sex field of the insert_one table.

Readers who are interested in more MySQL-related content can check out the following topics: "Summary of MySQL Common Functions", "Summary of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Summary of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock-Related Skills".

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Detailed explanation of the idea of ​​using Python sql statements to perform multi-condition fuzzy queries on mysql database
  • Essential conditional query statements for MySQL database
  • MySQL retrieves data based on the JSON field content as a query condition (including JSON arrays)
  • MySQL and PHP basics and applications: data query statements
  • Why does the index in the Mysql database table not improve the query speed?
  • MySQL insert data and query data
  • MySQL learning prerequisites for querying data

<<:  A tutorial on how to install, use, and automatically compile TypeScript

>>:  Tomcat security specifications (tomcat security reinforcement and specifications)

Recommend

Detailed tutorial on building a private Git server on Linux

1. Server setup The remote repository is actually...

How to disable foreign key constraint checking in MySQL child tables

Prepare: Define a teacher table and a student tab...

RHCE installs Apache and accesses IP with a browser

1. at is configured to write "This is a at t...

Docker container exits after running (how to keep running)

Phenomenon Start the Docker container docker run ...

How to deploy Redis 6.x cluster through Docker

System environment: Redis version: 6.0.8 Docker v...

Vue3 gets the current routing address

Correct answer Using useRouter : // router path: ...

JavaScript implements page scrolling animation

Table of contents Create a layout Add CSS styles ...

MySQL 5.7.15 installation and configuration method graphic tutorial (windows)

Because I need to install MySQL, I record the ins...

Several popular website navigation directions in the future

<br />This is not only an era of information...

Detailed explanation of the mechanism and implementation of accept lock in Nginx

Preface nginx uses a multi-process model. When a ...

How to install mysql in docker

I recently deployed Django and didn't want to...

Steps to set up HTTPS website based on Nginx

Table of contents Preface: Encryption algorithm: ...

How to import, register and use components in batches in Vue

Preface Components are something we use very ofte...