簡體   English   中英

SQL 從 2 個表插入數據

[英]SQL INSERT data from 2 tables

我有 2 個表, postcodelatlngbranch

postcodelatlng

郵政編碼 緯度 液化天然氣
AB10 1XG 57.1441650 -2.1148480
AB10 6RN 57.1378800 -2.1214870
AB10 7JB 57.1242740 -2.1271900
AB10 5QN 57.1427010 -2.0932950
AB10 6UL 57.1375470 -2.1122330

branch

分支 郵政編碼
1個 ZE2 9TL
4個 BB1 7DJ
9 YO8 9DW

我正在嘗試為branch中的每個郵政編碼創建一個新表,它列出 postcodelatlng 中的每個postcodelatlng

New table

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

我已經嘗試在 Python 中使用 Pandas 和 SQLAlchemy 執行此操作,但我無法理解這一點,所以我認為在 SQL 中執行此操作可能更容易,但我也堅持這樣做!

lat/lng 數據僅保存在postcodelatlng中,但這可以手動添加到branch表(那里只有 42 個唯一的郵政編碼(一些分支共享一個郵政編碼))

我在branch有 120 條記錄,在postcodelatlng有 42 個獨特的郵政編碼和 1778786 條記錄。

通過無條件連接,到達了所需的表,但我不知道 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

或交叉連接

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