簡體   English   中英

通過匹配SQL中的列值來聯接兩個表

[英]Join two tables by matching column values in SQL

嗨,我一直試圖通過它們的匹配值聯接兩個表。

我的第一個表名為“位置”的列:(國家,州,城市,緯度,經度)

我有第二個名為twitter的表,其列為:(用戶,Tweet,緯度,經度)

我想做的是將位置表加入到Twitter表中,當緯度和經度匹配時,它將在Twitter數據旁邊顯示相應的國家,州和城市列。

到目前為止,我還沒有嘗試過。 它所做的一切將每個城市添加到多行中的每個推文中。

select * from twitter
join location 
on twitter.latitude = location.latitude
and twitter.longitude = location.longitude

請幫忙!

由於可能沒有每個緯度/經度的位置,因此我們使用LEFT JOIN

select t.User
    ,t.Tweet
    ,t.Latitude
    ,t.Longitude
    ,l.Country
    ,l.State
    ,l.City
from twitter t
left join location l on t.latitude = l.latitude
    and t.longitude = l.longitude

使用左聯接。 它會給你所有的結果。

select * from twitter
left join location 
on twitter.latitude = location.latitude
and twitter.longitude = location.longitude
select * from twitter t
inner join location l
on t.latitude = l.latitude
WHERE t.longitude = l.longitude

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM