我有2张表格如下
client_table
date (timestamp)
ip (varchar)
value1 (int)
value2 (int)
ip_address_table
starting_ip (varchar)
ending_ip (varchar)
provider (varchar)
我正在尝试使用ip_address_table表查找client_table条目,以查找ip地址在范围内的每小时平均值。 (如果它不在范围内忽略该行) 以下是我的想法
SELECT avg(value1),avg(value2)
FROM client_table
WHERE (INET_ATON(ip) BETWEEN INET_ATON(starting_ip) AND INET_ATON(ending_ip))
GROUP BY DATE(date),HOUR(date)
但是,它没有给出正确的输出。我看了http://blog.jcole.us/2007/11/24/on-efficiently-geo-referencing-ips-with-maxmind-geoip-and-mysql-gis/但是很难写出一个存储函数来简化任务 任何帮助赞赏
由于
编辑:我的样本数据低于
client_table
"date" "ip" "value1" "value2"
"2012-01-10 06:38:29" "121.1.2.3" "1049" "301"
"2012-01-11 06:41:35" "58.1.2.3" "16453" "794"
"2012-01-12 06:43:50" "101.1.2.3" "1712" "410"
"2012-01-13 06:44:04" "121.1.10.3" "6594" "863"
"2012-01-14 06:45:00" "79.1.2.3" "879" "614"
ip_address_table
"starting_ip" "ending_ip" "provider"
"121.0.0.0" "121.255.255.255" provider1
"79.1.2.0" "79.1.2.255" provider1
....
答案 0 :(得分:4)
我没有看到你正在使用ip表,也许尝试这样的事情(未经测试):
SELECT avg(value1),avg(value2)
FROM client_table c, ip_address_table ip
WHERE (INET_ATON(ip) BETWEEN INET_ATON(starting_ip) AND INET_ATON(ending_ip))
GROUP BY DATE(date),HOUR(date)