简体   繁体   English

MySQL更新表使用另一个表中的数据

[英]mysql update table using data from another table

i have a table structures in my database like this: 我在数据库中有一个这样的表结构:

/*city*/
+----------+------------+
|    id    |   name     |
|-----------------------|
|    1     | Gotham     |
|    2     | Metropolis |
|    3     | Smallville |
|    4     | Fawcett    |
+----------+------------+   

/*district*/ 
+----------+------------+------------+
|    id    |   name     |   city_id  |
|------------------------------------|
|    1     |     A      |      1     |
|    2     |     B      |      1     |
|    3     |     C      |      2     |
|    4     |     D      |      2     |
|    5     |     E      |      2     |
|    6     |     F      |      3     |
|    7     |     G      |      3     |
|    8     |     H      |      4     |
+----------+------------+------------+

/*distance*/
+----------+-------------+------------------+-------------------------+---------+
|    id    | origin_city | city_destination |  district_destination   |  length |
|---------------------------------------------------------------------|---------|
|    1     |     2       |         2        |            1            |    4    |
|    2     |     3       |         3        |            1            |    5    |
|    3     |     1       |         1        |            2            |    6    |
|    4     |     2       |         2        |            3            |    5    |
|    5     |     4       |         4        |            1            |    8    |
|    6     |     4       |         2        |            4            |    9    |
|    7     |     4       |         3        |            5            |    11   |
|    8     |     1       |         4        |            6            |    13   |
+----------+-------------+------------------+-------------------------+---------+

the table district is connected to city table via city_id foreign key, and the distance table is connected to both city and district table, the problem is if in distance table, there are wrong city_destination data that don't match with the district_destination , i need to fix this, but i don't know how to use the update query for this kind of trouble, to show the wrong city_destination data i used this query: 表区通过city_id外键连接到城市表,而距离表同时连接到城市表和区表,问题是如果在距离表中,有错误的city_destination数据与district_destination不匹配,我需要解决此问题,但我不知道如何使用更新查询来解决此类问题,以显示错误的city_destination数据,我使用了以下查询:

SELECT a.* FROM distance a, district b WHERE a.district_destination = b.id AND a.city_destination != b.city_id

First, ditch the old-school comma syntax for the join operation. 首先,放弃老式的逗号语法进行联接操作。 Use the JOIN keyword and move the join predicates to an ON clause. 使用JOIN关键字并将连接谓词移至ON子句。 Write a SELECT query that returns the existing row to be updated (along with the PK, and the new value to be assigned. (Which looks to be as far as you got.) 编写一个SELECT查询,该查询返回要更新的现有行(连同PK和要分配的新值。(看起来就到您为止)。

Assuming that we want to replace the values in the city_destination column of distance table, and seeing that this this column is functionally dependent on district_destination ... 假设我们要替换distance表的city_destination列中的值,并且看到此列在功能上取决于district_destination ...

Start with a query that returns the rows to be updated. 从查询开始,该查询返回要更新的行。

SELECT ce.id                  AS id
     , ce.district_destination AS district_destination 
     , ce.city_destination    AS old_city_destination
     , ct.city_id             AS new_city_destination        
  FROM distance ce
  JOIN district ct
    ON ct.id = ce.district_destination
   AND NOT ( ct.city_id <=> ce.city_destination )
 ORDER BY ce.id

In MySQL, a multi-table update is pretty straightforward. 在MySQL中,多表更新非常简单。 The syntax is documented in the MySQL Reference Manual. 该语法记录在《 MySQL参考手册》中。

First, we'll write it as a SELECT, using the previous query as an inline view 首先,我们将之前的查询作为内联视图将其编写为SELECT

SELECT t.id
     , s.new_city_destination
  FROM ( SELECT ce.id                  AS id
              , ce.district_destination AS district_destination 
              , ce.city_destination    AS old_city_destination
              , ct.city_id             AS new_city_destination        
           FROM distance ce
           JOIN district ct
             ON ct.id = ce.district_destination
            AND NOT ( ct.city_id <=> ce.city_destination )
          ORDER BY ce.id
       ) s
  JOIN distance t
    ON t.id = s.id

Then we can convert that to an UPDATE statement. 然后,我们可以将其转换为UPDATE语句。 Replace SELECT ... FROM with UPDATE and add a SET clause at the end. UPDATE替换SELECT ... FROM并在末尾添加SET子句。 (Before the WHERE clause if there was one.) (在WHERE子句之前,如果有一个。)

UPDATE ( SELECT ce.id                  AS id
              , ce.district_destination AS district_destination 
              , ce.city_destination    AS old_city_destination
              , ct.city_id             AS new_city_destination        
           FROM distance ce
           JOIN district ct
             ON ct.id = ce.district_destination
            AND NOT ( ct.city_id <=> ce.city_destination )
          ORDER BY ce.id
       ) s
  JOIN distance t
    ON t.id = s.id
   SET t.city_destination = s.new_city_destination

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM