如何防止PHP中另一个foreach循环中的foreach循环重复?

时间:2017-01-27 10:36:53

标签: php mysql

我有一个音乐网站,每次用户点击播放时,我的PHP函数会插入表views

我正在尝试重新排序我的MySQL数据库以优化其性能,因此我尝试在此示例中计算按时间分组的每个track的总视图数。

仅选择表格views中的曲目:

$query = "SELECT  `track` FROM  `views` GROUP BY `track`";

$query的结果:

|track|
|-----|
|140  |
|125  |
|33   |
|...  |

将每个结果的行数计为播放并按日期分组:

$querysel = $this->db->query(sprintf("SELECT COUNT( * ) AS plays , `time`, `track` FROM `views` WHERE `track` = '%s' GROUP BY DATE( `time` )", $tid)); 

$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   |

最终在新表中插入结果:

$this->db->query(sprintf("INSERT INTO views_counts (tid,plays,time) VALUES ('%s','%s','%s')", $newtid, $plays, $time));

这是我的完整功能:

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));

        }

    }

}

在我的函数中,我使用GROUP BY DATE(时间),那么为什么最后一个查询插入到新表views_counts中的同时重复?

这是我的输出

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

如你所见,我同时有多个相同的结果。

这是我的预期:

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

更新

这就是我称之为“一次性”功能的方式:

<?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);
?>

函数countViews()位于classes.php内。

我称这个PHP只是通过网络访问所请求的页面。

4 个答案:

答案 0 :(得分:1)

我建议只使用一个查询解决您的问题。

function countViews() {
    $query = $this->db->query("SELECT COUNT( * ) AS plays , `time`, `track` FROM `views` GROUP BY `track`,DATE( `time` );"); 
    while($row = $query->fetch_assoc()) {
        $this->db->query(sprintf("INSERT INTO views_counts (tid,plays,time) VALUES ('%s','%s','%s')", $row['track'], $row['plays'], $row['time']));
    }
}

答案 1 :(得分:0)

试试这个:

function countViews() {

    $query = "SELECT  `track` FROM  `views` GROUP BY `track`";
    $result = $this->db->query($query);

    while($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }

    $rowsnew = array();

    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 $rownew1) {

        $newtid = $rownew1['track'];
        $plays = $rownew1['plays'];
        $time = $rownew1['time'];

        $this->db->query(sprintf("INSERT INTO views_counts (tid,plays,time) VALUES ('%s','%s','%s')", $newtid, $plays, $time));

    }

}

答案 2 :(得分:0)

在单个查询中真的可以完成吗?

INSERT INTO views_counts (tid, time, plays)
    SELECT       track, DATE(`time`), COUNT(*)
        FROM `views`
        GROUP BY track, DATE(`time`)

请注意,INSERT的行来自SELECT

我冒昧地使用DATE(time),否则,选择 time是任意的。

答案 3 :(得分:-1)

好的,所以只有一个SELECT查询就足够了,具体如下:

$query = "SELECT `track`, `time`, COUNT(*) AS `plays` FROM `views` 
                 GROUP BY `track`, DATE(`time`) 
                 ORDER BY `track`, DATE(`time`);"

$result = $this->db->query($query);

while($row = $result->fetch_assoc()) {
    $newtid  =  $row['track'];
    $plays   =  $row['plays'];
    $time    =  $row['time'];

    $this->db->query(sprintf("INSERT INTO views_counts (tid,plays,time) 
                                     VALUES ('%s','%s','%s')", 
                                     $newtid, $plays, $time));

}


这应该是理想的做法。使用手动创建的样本数据进行简单的查询演示:

create table views (track int, `time` timestamp);
insert into views VALUES(1, '2017-01-26 12:43:16');
insert into views VALUES(1, '2017-01-27 12:45:24');
insert into views VALUES(1, '2017-01-27 12:50:30');

insert into views VALUES(2, '2017-01-25 01:00:00');
insert into views VALUES(2, '2017-01-25 02:00:00');
insert into views VALUES(2, '2017-01-26 11:30:00');
insert into views VALUES(2, '2017-01-26 11:45:00');
insert into views VALUES(2, '2017-01-26 13:45:00');
insert into views VALUES(2, '2017-01-26 15:45:00');
insert into views VALUES(2, '2017-01-27 08:00:00');

insert into views VALUES(3, '2017-01-27 09:00:00');


SELECT `track`, `time`, COUNT(*) AS plays FROM views 
GROUP BY `track`, DATE(`time`) ORDER BY `track`, DATE(`time`);

使用此查询生成的输出,这是预期落入views_counts

的输出
track  |  time                |    plays   
----------------------------------------------
1      |  2017-01-26 12:43:16 |    1
1      |  2017-01-27 12:45:24 |    2
2      |  2017-01-25 01:00:00 |    2
2      |  2017-01-26 11:30:00 |    4
2      |  2017-01-27 08:00:00 |    1
3      |  2017-01-27 09:00:00 |    1

Demo link

希望有帮助...