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

Summary of using MySQL isolation columns and prefix indexes

Table of contents Isolate Data Columns Prefix Ind...

Detailed installation process of nodejs management tool nvm

nvm nvm is responsible for managing multiple vers...

Mysql 5.7.18 Using MySQL proxies_priv to implement similar user group management

Use MySQL proxies_priv (simulated role) to implem...

How to use js to communicate between two html windows

Scenario: When page A opens page B, after operati...

Some useful meta setting methods (must read)

<meta name="viewport" content="...

Summary of commonly used SQL in MySQL operation tables

1. View the types of fields in the table describe...

wget downloads the entire website (whole subdirectory) or a specific directory

Use wget command to download the entire subdirect...

Vue routing to implement login interception

Table of contents 1. Overview 2. Routing Navigati...

Summary of examples of common methods of JavaScript arrays

Table of contents Common array methods concat() M...

Example analysis of the page splitting principle of MySQL clustered index

This article uses an example to illustrate the pa...

Linux uses iftop to monitor network card traffic in real time

Linux uses iftop to monitor the traffic of the ne...

Use a table to adjust the format of the form controls to make them look better

Because I want to write a web page myself, I am al...

MySQL series tutorials for beginners

Table of contents 1. Basic concepts and basic com...