簡體   English   中英

MySQL更新表使用另一個表中的數據

[英]mysql update table using data from another table

我在數據庫中有一個這樣的表結構:

/*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   |
+----------+-------------+------------------+-------------------------+---------+

表區通過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

首先,放棄老式的逗號語法進行聯接操作。 使用JOIN關鍵字並將連接謂詞移至ON子句。 編寫一個SELECT查詢,該查詢返回要更新的現有行(連同PK和要分配的新值。(看起來就到您為止)。

假設我們要替換distance表的city_destination列中的值,並且看到此列在功能上取決於district_destination ...

從查詢開始,該查詢返回要更新的行。

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

在MySQL中,多表更新非常簡單。 該語法記錄在《 MySQL參考手冊》中。

首先,我們將之前的查詢作為內聯視圖將其編寫為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

然后,我們可以將其轉換為UPDATE語句。 UPDATE替換SELECT ... FROM並在末尾添加SET子句。 (在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