Get count and sum for each ip from mysql table -


im been looking generating ip address report 1 table , inserting have yet succeed or find resource allow me so.

my ip table contains many duplicate ip's , sum(minutes) of minutes , count(name) each distinct ip

ip                  name    minutes 199.199.199.199     nick    23 199.199.199.200     nick    3 199.199.199.200     bob     34 199.199.199.201     bob     56 199.199.199.201     sue     12 199.199.199.202     sue     45 199.199.199.202     helen   65 199.199.199.202     helen   15 

my current method following

select distinct(distinct) ip `ip.ip_stats`; 

in php, add each of distinct ip addreses array

$arr = array("199.199.199.199","199.199.199.200","199.199.199.201","199.199.199.202"); 

once array generated run through each in array

foreach($arr $ip){      sql ="select count(name) cnt, sum(minutes) sum `ip.ip_stats` ip='$ip'";      $result = mysqli_query($con, $query);       while ($row = mysqli_fetch_assoc($result)) {           mysqli_query($con, "insert ip.ip_report ( ip, count, sum )              values ( $ip, $cnt, $sum )       } } 

so after getting sum , count of each ip in array loop through , insert report table.

my main problem sheer size , amount of time take run, have on ten million rows of data , 3-5 per minutes (large table example).

is there way use mysql select distinct , generate report? there way of achieving following query? using loop or while?

select sum(minutes), count(name) ( select distinct(ip) ip.ip_stats) ? 

i believe after:

select s.ip, s.name, sum(s.minutes) totalminutes, count(s.name) namecount ip_stats s group s.ip, s.name 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -