vel*_*ter 2 sql join criteria database-table
希望有人能弄清楚我在这里做错了什么.任务似乎很简单,但显然超出了我的范围.
我有两张桌子,我正试图追加降水数据.需要在两个表中匹配三个条件以获得每个现场位置的正确降水数据,例如年,纬度和经度.我尝试使用以下查询(其他失败的查询,我甚至不记得了):
SELECT f.*, g.*
FROM fieldSites f LEFT OUTER JOIN gpcp_precipitation2 g
ON f.date = g.year
AND f.d_lat = g.lat
WHERE f.d_lon = g.lon;
Run Code Online (Sandbox Code Playgroud)
这一次超时了
和:
SELECT *
FROM fieldSites
INNER JOIN gpcp_precipitation2
ON (fieldSites.d_lon = gpcp_precipitation2.lon
AND fieldSites.d_lat = gpcp_precipitation2.lat
AND fieldSites.date = gpcp_precipitation2.year);
Run Code Online (Sandbox Code Playgroud)
这个也超时了.
我想转储到.csv文件,但是现在我只想执行一个成功的查询.
这是我的表格:
left table: fieldSites
siteId d_lat d_lon year data1 data2 country
1 -13.75 18.75 2009 0.598 0.351 Angola
1 -13.75 18.75 2008 0.654 0.330 Angola
1 -13.75 18.75 2007 0.489 0.381 Angola
1 -13.75 18.75 2006 0.554 0.389 Angola
1 -13.75 18.75 2005 0.321 0.321 Angola
1 -13.75 18.75 2004 0.598 0.351 Angola
1 -13.75 18.75 2003 0.654 0.330 Angola
1 -13.75 18.75 2002 0.489 0.381 Angola
1 -13.75 18.75 2001 0.554 0.389 Angola
2 -78.75 163.75 2009 0.285 0.155 Antarctica
2 -78.75 163.75 2008 0.285 0.155 Antarctica
2 -78.75 163.75 2007 0.285 0.155 Antarctica
2 -78.75 163.75 2006 0.285 0.155 Antarctica
2 -78.75 163.75 2005 0.285 0.155 Antarctica
...1052 sites, 11 years, 11496 rows
right table: gpcp_precipitation2
siteId lat lon year precipitation
1 81.5 1.25 2009 93.36571912
1 81.5 1.25 2008 93.36571912
1 81.5 1.25 2007 93.36571912
1 81.5 1.25 2006 93.36571912
1 81.5 1.25 2005 93.36571912
1 81.5 1.25 2004 93.36571912
1 81.5 1.25 2003 93.36571912
1 81.5 1.25 2002 93.36571912
1 81.5 1.25 2001 93.36571912
1 81.5 1.25 2000 93.36571912
1 81.5 3.75 2009 93.36571912
1 81.5 3.75 2008 93.36571912
1 81.5 3.75 2007 93.36571912
... 92300 rows
Run Code Online (Sandbox Code Playgroud)
我想要的是这个:
siteId d_lat d_lon year data1 data2 country precipitation
1 13.75 18.75 2009 0.598 0.351 Angola 144.286
1 13.75 18.75 2008 0.654 0.330 Angola 114.970
1 13.75 18.75 2007 0.489 0.381 Angola 70.000
1 13.75 18.75 2006 0.554 0.389 Angola 174.179
1 13.75 18.75 2005 0.321 0.321 Angola 174.743
1 13.75 18.75 2004 0.598 0.351 Angola 70.506
1 13.75 18.75 2003 0.654 0.330 Angola 173.716
1 13.75 18.75 2002 0.489 0.381 Angola 74.162
1 13.75 18.75 2001 0.554 0.389 Angola 139.445
2 78.75 163.75 2009 0.285 0.155 Antarctica 0
2 78.75 163.75 2008 0.285 0.155 Antarctica 0
2 78.75 163.75 2007 0.285 0.155 Antarctica 0
2 78.75 163.75 2006 0.285 0.155 Antarctica 0
Run Code Online (Sandbox Code Playgroud)
我做的事情完全是愚蠢的吗?我很难过.非常感谢任何建议.
Select fieldSites.*, precipitation.*
From fieldSites
Inner Join gpcp_precipitation2 As precipitation On precipitation.siteId = fieldSites.siteId
Where
fieldSites.d_year = precipitation.year And
fieldSites.d_lat = precipitation.lat And
fieldSites.d_lon = precipitation.lon
Run Code Online (Sandbox Code Playgroud)
如果该查询超时,则会出现索引问题,不一定是查询问题.这会在where子句中为您提供几个谓词来进行过滤,因此它应该会减少您的连接,但是您可能需要一个包含两个表上的siteId,year,lat和lon的索引.
| 归档时间: |
|
| 查看次数: |
7629 次 |
| 最近记录: |