MySQL UPDATE和JOIN在同一列上

时间:2018-08-24 09:21:09

标签: mysql

我有两个桌子

directory_country

|country_id |iso2_code |iso3_code
|AD         |AD        |AND
|AE         |AE        |ARE
|AF         |AF        |AFG
|AG         |AG        |ATG
...

customer_address_entity_varchar

|value_id |entity_type_id |attribute_id |entity_id |value
|1        |2              |19           |1         |asdad
|2        |2              |20           |1         |Petya
|3        |2              |21           |1         |NULL
|4        |2              |22           |1         |Vasin
|5        |2              |23           |1         |asdasdasd
|6        |2              |24           |1         |asdasd
|7        |2              |26           |1         |KKK
|8        |2              |27           |1         |AFG
|9        |2              |28           |1         |American Samoa
|18       |2              |27           |2         |ARE
|35       |2              |28           |3         |Georgia
|36       |2              |30           |3         |123
|37       |2              |27           |3         |US
|38       |2              |31           |3         |133

我需要将customer_address_entity_varchar.value更改为相应的directory_country.country_id,其中customer_address_entity_varchar.value = directory_country.iso3_codecustomer_address_entity_varchar.attribute_id = 27。 这意味着将customer_address_entity_varchar中的ARE更改为AE,将AFG更改为AF。

我研究了Mysql Update教程并构建了查询,但是它不起作用并且没有显示错误。 查询

UPDATE `customer_address_entity_varchar` 
INNER JOIN `directory_country` 
  ON customer_address_entity_varchar.value = directory_country.iso3_code 
SET `value`= directory_country.country_id 
WHERE `attribute_id` = 27;

感谢您的解决方案。

1 个答案:

答案 0 :(得分:0)

PhpMyAdmin遇到了一些麻烦,查询是正确的。我将在此处留下正确答案。可能会帮助别人。

UPDATE `customer_address_entity_varchar` 
INNER JOIN `directory_country` 
  ON customer_address_entity_varchar.value = directory_country.iso3_code 
SET `value`= directory_country.country_id 
WHERE `attribute_id` = 27;

说明明确的教程: http://www.mysqltutorial.org/mysql-update-join/