MySQL - 如何按另一个查询的结果进行分组?
我有一个访问表(visit_table),结构:
- ID
- IP_address
- 时间戳
和一个表(country_table),其中包含IP范围的国家名称
- ID
- begin_IP(IP范围的开始)
- end_IP(IP范围的结束)
- begin_num(begin_IP的数值等效,即INET_ATON(begin_IP))
- end_num
- 国家/地区代码(例如美国)
- 国家/地区名称
(基于 http://www.maxmind.com/app/csv )
我正在使用MySQL 5.0.67 并想编写一个查询来返回按国家/地区划分的访问次数,因此大致如下:
SELECT country.table.country_name,
count(distinct visit_table.IP_address)
FROM country_table, visit_table
WHERE country_name = (SELECT country_name
FROM country_table
WHERE INET_ATON(visits.IP_address) >= begin_num
AND INET_ATON( visits.IP_Address) <= end_num
LIMIT 0,1 )
GROUP BY country_table.country_name
我很确定我做错了什么,但不知道如何修复 - 某种JOIN 或使用 SELECT 或 GROUP BY 语句执行某些操作?欢迎任何指点!
I have a visit table (visit_table), structure:
- ID
- IP_address
- timestamp
and a table (country_table) with country names for IP ranges
- ID
- begin_IP (start of IP range)
- end_IP (end of IP range)
- begin_num (the numerical equivalent of begin_IP, i.e. INET_ATON(begin_IP))
- end_num
- country_code (e.g. US)
- country_name
(based on http://www.maxmind.com/app/csv )
I'm using MySQL 5.0.67 and would like to write a query to return the number of visits by country, so something along the lines of:
SELECT country.table.country_name,
count(distinct visit_table.IP_address)
FROM country_table, visit_table
WHERE country_name = (SELECT country_name
FROM country_table
WHERE INET_ATON(visits.IP_address) >= begin_num
AND INET_ATON( visits.IP_Address) <= end_num
LIMIT 0,1 )
GROUP BY country_table.country_name
I'm pretty sure I'm doing something wrong but don't know how to fix - some sort of JOIN or doing something with the SELECT or GROUP BY statement? Any pointers welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用:
如果该 IP 范围没有国家/地区,您将无法获得任何内容。但是,如果从该范围返回多个国家/地区,则需要更新查询来处理它。
如果您想要一个可能没有为其捕获 IP 地址的国家/地区,请使用:
Use:
If there's no country for that IP range, you won't get anything. But if there's more than one country returned from the range, the query needs to be updated to handle it.
If you want a country that might not have ip addresses captured for it, use:
是的,您想要加入:
Yes you want a join: