为什么这个 Yahoo weather API 查询不起作用?
Why does this Yahoo weather API query not work?
获取渥太华天气预报的查询无效:
SELECT * FROM weather.forecast WHERE woeid IN (SELECT woeid FROM geo.places WHERE text="Ottawa, Canada" limit 1)
结果:
"query": {
"count": 1,
"created": "2015-07-02T13:06:45Z",
"lang": "en-US",
"results": {
"channel": {
"title": "Yahoo! Weather - Error",
"description": "Yahoo! Weather Error",
"item": {
"title": "City not found",
"description": "\nInvalid Input /forecastrss?w=91982014\n"
}
}
}
}
自行执行,子查询得到正确结果:
SELECT woeid FROM geo.places WHERE text="Ottawa, Canada" limit 1
结果:
"query": {
"count": 1,
"created": "2015-07-02T13:00:47Z",
"lang": "en-US",
"results": {
"place": {
"woeid": "91982014"
}
}
}
这适用于我尝试过的所有其他城市,当我遗漏 "limit 1" 它也适用于渥太华,但 returns 结果太多。是我做错了什么还是 API 有问题?
我认为问题在于渥太华有多个结果(多个 WOEID),并且并非所有这些 WOEID 在 weather.forecast table 中都有相应的条目。我认为 是因为其中一些条目的水平与我们预测的水平不同。
查询 SELECT woeid FROM geo.places WHERE text="Ottawa, Canada"
产生以下结果:
"results": {
"place": [
{
"woeid": "91982014"
},
{
"woeid": "29375164"
},
{
"woeid": "12483153"
}
]
}
如果您修改查询以包括地点类型 SELECT woeid, placeTypeName.content FROM geo.places WHERE text="Ottawa, Canada"
,您可以看到结果包括城镇、County/District 和岛屿,城镇是第一个结果中的项目:
"results": {
"place": [
{
"woeid": "91982014",
"placeTypeName": "Town"
},
{
"woeid": "29375164",
"placeTypeName": "County/District"
},
{
"woeid": "12483153",
"placeTypeName": "Island"
}
]
}
在 returns 太多结果 SELECT * FROM weather.forecast WHERE woeid IN (SELECT woeid FROM geo.places WHERE text="Ottawa, Canada")
的完整查询的结果中,您可以看到一个名为 diagnostics
的部分,其中有一个名为 url
的数组,它给出天气 API 作为查询结果调用的 URL:
http://weather.yahooapis.com/forecastrss?w=12483153
http://weather.yahooapis.com/forecastrss?w=29375164
http://weather.yahooapis.com/forecastrss?w=91982014
并且 91982014 的结果是一个错误:
您可以从结果中删除错误案例 SELECT * FROM weather.forecast WHERE woeid IN (SELECT woeid FROM geo.places WHERE text="Ottawa, Canada") AND title NOT LIKE "%Error%"
,这仍然会给出两个结果。
看来你真正想做的是意识到加拿大有三个名字中有"Ottawa"的地方:省会城市渥太华安大略省、安大略省的 county/district 渥太华,以及努纳武特地区称为渥太华群岛的一系列岛屿。因此,您需要更具体地查询。
SELECT * FROM weather.forecast WHERE woeid IN (SELECT woeid FROM geo.places WHERE text="Ottawa, Ontario, Canada") AND title NOT LIKE "%Error%"
感觉有点乱,但它给出了一个结果。
获取渥太华天气预报的查询无效:
SELECT * FROM weather.forecast WHERE woeid IN (SELECT woeid FROM geo.places WHERE text="Ottawa, Canada" limit 1)
结果:
"query": {
"count": 1,
"created": "2015-07-02T13:06:45Z",
"lang": "en-US",
"results": {
"channel": {
"title": "Yahoo! Weather - Error",
"description": "Yahoo! Weather Error",
"item": {
"title": "City not found",
"description": "\nInvalid Input /forecastrss?w=91982014\n"
}
}
}
}
自行执行,子查询得到正确结果:
SELECT woeid FROM geo.places WHERE text="Ottawa, Canada" limit 1
结果:
"query": {
"count": 1,
"created": "2015-07-02T13:00:47Z",
"lang": "en-US",
"results": {
"place": {
"woeid": "91982014"
}
}
}
这适用于我尝试过的所有其他城市,当我遗漏 "limit 1" 它也适用于渥太华,但 returns 结果太多。是我做错了什么还是 API 有问题?
我认为问题在于渥太华有多个结果(多个 WOEID),并且并非所有这些 WOEID 在 weather.forecast table 中都有相应的条目。我认为 是因为其中一些条目的水平与我们预测的水平不同。
查询 SELECT woeid FROM geo.places WHERE text="Ottawa, Canada"
产生以下结果:
"results": {
"place": [
{
"woeid": "91982014"
},
{
"woeid": "29375164"
},
{
"woeid": "12483153"
}
]
}
如果您修改查询以包括地点类型 SELECT woeid, placeTypeName.content FROM geo.places WHERE text="Ottawa, Canada"
,您可以看到结果包括城镇、County/District 和岛屿,城镇是第一个结果中的项目:
"results": {
"place": [
{
"woeid": "91982014",
"placeTypeName": "Town"
},
{
"woeid": "29375164",
"placeTypeName": "County/District"
},
{
"woeid": "12483153",
"placeTypeName": "Island"
}
]
}
在 returns 太多结果 SELECT * FROM weather.forecast WHERE woeid IN (SELECT woeid FROM geo.places WHERE text="Ottawa, Canada")
的完整查询的结果中,您可以看到一个名为 diagnostics
的部分,其中有一个名为 url
的数组,它给出天气 API 作为查询结果调用的 URL:
http://weather.yahooapis.com/forecastrss?w=12483153
http://weather.yahooapis.com/forecastrss?w=29375164
http://weather.yahooapis.com/forecastrss?w=91982014
并且 91982014 的结果是一个错误:
您可以从结果中删除错误案例 SELECT * FROM weather.forecast WHERE woeid IN (SELECT woeid FROM geo.places WHERE text="Ottawa, Canada") AND title NOT LIKE "%Error%"
,这仍然会给出两个结果。
看来你真正想做的是意识到加拿大有三个名字中有"Ottawa"的地方:省会城市渥太华安大略省、安大略省的 county/district 渥太华,以及努纳武特地区称为渥太华群岛的一系列岛屿。因此,您需要更具体地查询。
SELECT * FROM weather.forecast WHERE woeid IN (SELECT woeid FROM geo.places WHERE text="Ottawa, Ontario, Canada") AND title NOT LIKE "%Error%"
感觉有点乱,但它给出了一个结果。