简体   繁体   English

用新的外键值更改SQL表

[英]Alter sql table with new foreign key values

I have 'locations' database and two tables. 我有“位置”数据库和两个表。 My first table was 'uszips' which looks like: 我的第一个表格是“ uszips”,看起来像:

--id--zipCode--stateCode--latitude--longitude--
   1    35004         AL  33.584132 -86.515570
   2    35005         AL  33.588437 -86.959727

Now, I have a second table called 'usstates' and I have 2-letter state codes already there: 现在,我有第二个表称为“ usstates”,并且已经有两个字母的状态代码:

--id--stateCode--
   1         AK
   2         AL

I was not able to write a query to modify 'uszips' table as 'stateCode' column would be 'stateId' to be foreign keys of the 'usstates'. 我无法编写查询来修改“ uszips”表,因为“ stateCode”列将是“ stateId”作为“ usstates”的外键。 For example: 例如:

--id--zipCode--stateId--latitude--longitude--
   1    35004        2  33.584132 -86.515570
   2    35005        2  33.588437 -86.959727

My best try is: 我最好的尝试是:

update uszips set uszips.stateCode=usstates.id
from uszips
join usstates on (uszips.stateCode=usstates.state)

But I recieve the following error: 但我收到以下错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from uszips join usstates on (uszips.stateCode=usstates.state)' at line 2

The set clause should come after the join clause: set子句应该 join子句之后:

UPDATE uszips
JOIN   usstates ON uszips.stateCode = usstates.state
SET    uszips.stateCode = usstates.id

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

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