简体   繁体   English

SQL 从 2 个表插入数据

[英]SQL INSERT data from 2 tables

I have 2 tables, postcodelatlng and branch .我有 2 个表, postcodelatlngbranch

postcodelatlng

postcode邮政编码 lat纬度 lng液化天然气
AB10 1XG AB10 1XG 57.1441650 57.1441650 -2.1148480 -2.1148480
AB10 6RN AB10 6RN 57.1378800 57.1378800 -2.1214870 -2.1214870
AB10 7JB AB10 7JB 57.1242740 57.1242740 -2.1271900 -2.1271900
AB10 5QN AB10 5QN 57.1427010 57.1427010 -2.0932950 -2.0932950
AB10 6UL AB10 6UL 57.1375470 57.1375470 -2.1122330 -2.1122330

branch

branch分支 postcode邮政编码
1 1个 ZE2 9TL ZE2 9TL
4 4个 BB1 7DJ BB1 7DJ
9 9 YO8 9DW YO8 9DW

I'm trying to create a new table that for every postcode in branch , it lists every postcode in postcodelatlng .我正在尝试为branch中的每个邮政编码创建一个新表,它列出 postcodelatlng 中的每个postcodelatlng

New table

from to from_lat从_lat from_lng来自_lng to_lat到_lat to_lng to_lng
ZE2 9TL ZE2 9TL AB10 1XG AB10 1XG 60.4481370 60.4481370 -1.1943700 -1.1943700 57.1441650 57.1441650 2.1148480 2.1148480
ZE2 9TL ZE2 9TL AB10 6RN AB10 6RN 60.4481370 60.4481370 -1.1943700 -1.1943700 57.1378800 57.1378800 -2.1214870 -2.1214870
ZE2 9TL ZE2 9TL AB10 7JB AB10 7JB 60.4481370 60.4481370 -1.1943700 -1.1943700 57.1242740 57.1242740 -2.1271900 -2.1271900
ZE2 9TL ZE2 9TL AB10 5QN AB10 5QN 60.4481370 60.4481370 -1.1943700 -1.1943700 57.1427010 57.1427010 -2.0932950 -2.0932950
ZE2 9TL ZE2 9TL AB10 6UL AB10 6UL 60.4481370 60.4481370 -1.1943700 -1.1943700 57.1375470 57.1375470 -2.1122330 -2.1122330
BB1 7DJ BB1 7DJ AB10 1XG AB10 1XG 53.7490640 53.7490640 -2.4843190 -2.4843190 57.1441650 57.1441650 2.1148480 2.1148480
BB1 7DJ BB1 7DJ AB10 6RN AB10 6RN 53.7490640 53.7490640 -2.4843190 -2.4843190 57.1378800 57.1378800 -2.1214870 -2.1214870
BB1 7DJ BB1 7DJ AB10 7JB AB10 7JB 53.7490640 53.7490640 -2.4843190 -2.4843190 57.1242740 57.1242740 -2.1271900 -2.1271900
BB1 7DJ BB1 7DJ AB10 5QN AB10 5QN 53.7490640 53.7490640 -2.4843190 -2.4843190 57.1427010 57.1427010 -2.0932950 -2.0932950
BB1 7DJ BB1 7DJ AB10 6UL AB10 6UL 53.7490640 53.7490640 -2.4843190 -2.4843190 57.1375470 57.1375470 -2.1122330 -2.1122330
YO8 9DW YO8 9DW AB10 1XG AB10 1XG 53.7743390 53.7743390 -1.0714240 -1.0714240 57.1441650 57.1441650 2.1148480 2.1148480
YO8 9DW YO8 9DW AB10 6RN AB10 6RN 53.7743390 53.7743390 -1.0714240 -1.0714240 57.1378800 57.1378800 -2.1214870 -2.1214870
YO8 9DW YO8 9DW AB10 7JB AB10 7JB 53.7743390 53.7743390 -1.0714240 -1.0714240 57.1242740 57.1242740 -2.1271900 -2.1271900
YO8 9DW YO8 9DW AB10 5QN AB10 5QN 53.7743390 53.7743390 -1.0714240 -1.0714240 57.1427010 57.1427010 -2.0932950 -2.0932950
YO8 9DW YO8 9DW AB10 6UL AB10 6UL 53.7743390 53.7743390 -1.0714240 -1.0714240 57.1375470 57.1375470 -2.1122330 -2.1122330

I've tried doing this in Python with Pandas and SQLAlchemy but I can't get my head around that so I thought it may be easier to just do it in SQL, but I'm also stuck on that!我已经尝试在 Python 中使用 Pandas 和 SQLAlchemy 执行此操作,但我无法理解这一点,所以我认为在 SQL 中执行此操作可能更容易,但我也坚持这样做!

The lat/lng data is only held in the postcodelatlng but this can be added manually to the branch table (only 42 unique postcodes there(some branches share a postcode)) lat/lng 数据仅保存在postcodelatlng中,但这可以手动添加到branch表(那里只有 42 个唯一的邮政编码(一些分支共享一个邮政编码))

I have 120 records in branch , 42 unique postcodes and 1778786 records on postcodelatlng .我在branch有 120 条记录,在postcodelatlng有 42 个独特的邮政编码和 1778786 条记录。

With an unconditional join, the desired table was reached, but I do not know the values of from_lat and from_lng通过无条件连接,到达了所需的表,但我不知道 from_lat 和 from_lng 的值

insert into newtable
select b.postcode,p.postcode,"from_lat = ?","from_lng = ?",p.lat,p.lng
from branch b join postcodelatlng p on 1 = 1

or cross join或交叉连接

insert into newtable
select b.postcode,p.postcode,"from_lat = ?","from_lng = ?",p.lat,p.lng
from branch b cross join postcodelatlng p

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

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