I have a music website where each time a user click on play, my PHP function insert into table views
I'm trying to reorder my MySQL database to optimize it in performance, so I tried in this example to count the total of views for each track grouped by time.
Select only tracks that are in table views:
$query = "SELECT  `track` FROM  `views` GROUP BY `track`";
The result for $query:
|track|
|-----|
|140  |
|125  |
|33   |
|...  |
Count the number of rows for each result as plays and group by date:
$querysel = $this->db->query(sprintf("SELECT COUNT( * ) AS plays , `time`, `track` FROM `views` WHERE `track` = '%s' GROUP BY DATE( `time` )", $tid)); 
The result for $querysel
|plays |time                |track |
|------|--------------------|------|
|82    |2016-12-26 18:20:16 |140   |
|1     |2017-01-10 15:52:55 |140   |
|2     |2017-01-26 13:17:25 |140   |
Final insert in a new table the result:
$this->db->query(sprintf("INSERT INTO views_counts (tid,plays,time) VALUES ('%s','%s','%s')", $newtid, $plays, $time));
Here is my complete function:
function countViews() {
    $query = "SELECT  `track` FROM  `views` GROUP BY `track`";
    $result = $this->db->query($query);
    while($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }
    foreach($rows as $row) {
        $tid = $row['track'];
        $querysel = $this->db->query(sprintf("SELECT COUNT( * ) AS plays , `time`, `track` FROM `views` WHERE `track` = '%s' GROUP BY DATE( `time` )", $tid)); 
        while($rownew = $querysel->fetch_assoc()) {
            $rowsnew[] = $rownew;
        }
        foreach($rowsnew as $rownew) {
            $newtid = $rownew['track'];
            $plays = $rownew['plays'];
            $time = $rownew['time'];
            $this->db->query(sprintf("INSERT INTO views_counts (tid,plays,time) VALUES ('%s','%s','%s')", $newtid, $plays, $time));
        }
    }
}
In my function I use GROUP BY DATE(time) ,so why the final query insert into the new table views_counts duplicates with the same time?
Here is my output:
id  | tid | plays | time
----|-----|-------|--------------
1   | 1   | 2     | 2017-01-26 12:43:16
2   | 1   | 1     | 2017-01-27 12:45:24
3   | 1   | 2     | 2017-01-26 12:43:16
4   | 1   | 1     | 2017-01-27 12:45:24
5   | 3   | 30    | 2016-12-26 18:20:16
6   | 1   | 2     | 2017-01-26 12:43:16
7   | 1   | 1     | 2017-01-27 12:45:24
8   | 1   | 2     | 2017-01-26 12:43:16
9   | 1   | 1     | 2017-01-27 12:45:24
10  | 3   | 30    | 2016-12-26 18:20:16
11  | 1   | 2     | 2017-01-26 12:43:16
12  | 1   | 1     | 2017-01-27 12:45:24
13  | 1   | 2     | 2017-01-26 12:43:16
14  | 1   | 1     | 2017-01-27 12:45:24
15  | 3   | 30    | 2016-12-26 18:20:16
As you see I have multiple same results for the same time.
Here is what I expected:
id  | tid | plays | time
----|-----|-------|--------------
1   | 1   | 2     | 2017-01-26 12:43:16
2   | 1   | 1     | 2017-01-27 12:45:24
3   | 3   | 30    | 2016-12-26 18:20:16
UPDATE
Here it is how I call this "one time" function:
<?php
    include("/var/www/html/includes/config.php");
    include("/var/www/html/includes/classes.php");
    session_start();
    $db = new mysqli($CONF['host'], $CONF['user'], $CONF['pass'], $CONF['name']);
    if ($db->connect_errno) {
        echo "Failed to connect to MySQL: (" . $db->connect_errno . ") " . $db->connect_error;
    }
    $db->set_charset("utf8");
    $feed = new feed();
    $feed->db = $db;                
    $result = $feed->countViews();
    mysqli_close($db);
?>
The function countViews() is inside classes.php. 
I call this PHP just going to visit the requested page through the web.
 
     
     
    