查询如下所示:
SELECT apninfo.zip,
CASE apninfo.zip
WHEN (SELECT zip F
FROM zipcode, apninfo
WHERE zipcode.zip = apninfo.zip
) THEN "SD"
WHEN (apninfo.zip > 9001 AND apninfo.zip < 96162) THEN "CA"
ELSE "US"
END AS LATION
FROM apinfo;
首先,它尝试查找邮政编码表,看看邮政编码是否在圣地亚哥。如果没有,则检查CA.此外,它假定它是美国邮政编码。
MySQL抛出错误。你能帮忙吗?
答案 0 :(得分:1)
认为你可以做那样的事情(我不喜欢迈克尔B.,我必须承认我不知道美国邮政编码,所以只需从你的查询中获取)。
select a.zip,
case when z.zip is not null then 'SD'
when a.zip between 9001 and 96162 then 'CA'
else 'US'
end as location
FROM apinfo a
left join zipcode z on z.zip = a.zip