簡體   English   中英

如何讓MySQL從第一個表中的第二個表中存儲查詢結果?

[英]How can I get MySQL to store the result of a query from a second table in the first one?

我有一份商務中心清單。 有時,數據輸入人員沒有所有信息,例如缺少城市名稱,但其余信息已完成。 我有另一個zipcodes表。 我希望能夠為那些沒有城市的地址進行郵政編碼查找,並獲取城市名稱。 我在MySQL中這樣做。

我在為這個問題獲取MySQL方面遇到了困難。 我不知道這是一個語法問題還是MySQL的邏輯是錯誤的。 這就是我所擁有的:

update centers city
set centers.city = (
select zipcode_types.primary_city
from centers, zipcode_types 
where centers.city="" and centers.zipcode=zip);

這是我從上面的MySQL得到的錯誤:

ERROR 1093 (HY000): You can't specify target table 'city' for update in FROM clause

我正在嘗試做的是從zipcode_types表中找到城市名稱,並在中心表中更新缺少的城市名稱。 感謝您的幫助,謝謝!

您可以使用多表更新語法

update centers 
  inner join zipcode_types on zipcode_types.zip = centers.zipcode
set centers.city = zipcode_types.primary_city
where centers.city='';

嘗試重寫您的更新語句,如下所示:

update centers set city=(select zipcode_types.primary_city from zipcode_types where enters.zipcode=zipcode_types.zip)
where centers.city=''

在這個SQL Fiddle頁面上顯示: http ://sqlfiddle.com/#!2/3f7b5/1

試一試:

UPDATE centers SET city = (
SELECT primary_city
FROM zipcode_types 
WHERE zipcode=city.zip) 
WHERE city IS null 
OR city ==''';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM