MySQL - 如何按另一个查询的结果进行分组?

发布于 2024-10-03 04:46:53 字数 1042 浏览 4 评论 0原文

我有一个访问表(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭白衣梦中忆 2024-10-10 04:46:53

使用:

  SELECT c.country_name, 
         COUNT(DISTINCT v.IP_address)  
    FROM COUNTRY_TABLE c
    JOIN VISIT_TABLE v ON v.country_name = c.country_name
   WHERE INET_ATON(v.IP_Address) BETWEEN begin_num AND end_num  
GROUP BY c.country_name

如果该 IP 范围没有国家/地区,您将无法获得任何内容。但是,如果从该范围返回多个国家/地区,则需要更新查询来处理它。

如果您想要一个可能没有为其捕获 IP 地址的国家/地区,请使用:

   SELECT c.country_name, 
          COUNT(DISTINCT v.IP_address)  
     FROM COUNTRY_TABLE c
LEFT JOIN VISIT_TABLE v ON v.country_name = c.country_name
    WHERE INET_ATON(v.IP_Address) BETWEEN begin_num AND end_num  
 GROUP BY c.country_name

Use:

  SELECT c.country_name, 
         COUNT(DISTINCT v.IP_address)  
    FROM COUNTRY_TABLE c
    JOIN VISIT_TABLE v ON v.country_name = c.country_name
   WHERE INET_ATON(v.IP_Address) BETWEEN begin_num AND end_num  
GROUP BY c.country_name

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:

   SELECT c.country_name, 
          COUNT(DISTINCT v.IP_address)  
     FROM COUNTRY_TABLE c
LEFT JOIN VISIT_TABLE v ON v.country_name = c.country_name
    WHERE INET_ATON(v.IP_Address) BETWEEN begin_num AND end_num  
 GROUP BY c.country_name
江湖正好 2024-10-10 04:46:53

是的,您想要加入:

SELECT country_table.country_name, count(distinct visit_table.IP_address)  
FROM country_table
RIGHT JOIN visit_table ON visit_table.id=country_table.id       
GROUP BY country_table.country_name  

Yes you want a join:

SELECT country_table.country_name, count(distinct visit_table.IP_address)  
FROM country_table
RIGHT JOIN visit_table ON visit_table.id=country_table.id       
GROUP BY country_table.country_name  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文