简体   繁体   English

更新 SQL 表将 varchar 转换为 int

[英]Updating SQL table converting varchar to int

I currently have a table new_funeralhomes as below, which has the new data i wish to add to another table:我目前有一个如下表new_funeralhomes ,其中包含我希望添加到另一个表的新数据:

在此处输入图像描述 , ,

Within that table, i can easily send address,postal,phone, website and email to this table addressTest :在该表中,我可以轻松地将地址、邮政、电话、网站和 email 发送到该表addressTest

在此处输入图像描述 . .

However as you can see by the new_funeralhomes table, the city/state are text value and in the second table ( addressTest ) they are int values connecting to these tables:但是,正如您在new_funeralhomes表中看到的那样,城市/州是文本值,在第二个表( addressTest )中它们是连接到这些表的 int 值:

city : city

在此处输入图像描述 , ,

province : province

在此处输入图像描述 . .

How would i go about updating the addressTest table with the new city and province?我将如何使用新的城市和省份更新addressTest表? The iid and id values match up and are the same throughout. iid 和 id 值匹配并且始终相同。

Im newer to this so any help would be appreciated, thank you.我对此较新,因此将不胜感激,谢谢。

You seem to be looking for an insert... select statement with join s.您似乎正在寻找insert... select带有join的语句。 The logic is:逻辑是:

insert into addressTest
    (address, province, postcalcode, city, primaryphone, secondaryphoe, website)
select
    n.address
    p.id,
    n.postcal,
    c.id,
    n.phone,
    n.phone2,
    n.website
from new_funeralhomes n
inner join city c on c.name = n.city
inner join province p on p.name = n.state

Side note: it seems like the source table has less columns than the target;旁注:源表的列数似乎少于目标表; I adapted the column list for what seem to be the matching columns - you might need to review that so it properly matches your requirement (but the join logic, which is the heart of your question, stays the same).我为似乎匹配的列调整了列列表 - 您可能需要查看它,以便它正确匹配您的要求(但join逻辑,这是您问题的核心,保持不变)。

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

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