在一天内获得用户查看次数最多的网址

时间:2016-08-10 10:26:46

标签: mysql unix mysqli

我有一个像这样的结构的表

id time       userid ip              course module cmid action url                     info
 1 1441006367      2 110.142.152.217      1 user      0 update view.php?id=2&course=1 
 2 1441006397      2 110.142.152.217      1 course    0 view   view.php?id=1              1
 3 1441061491      2 110.142.152.217      1 user      0 view   view.php?id=0&course=1     2
 4 1441061491      2 110.142.152.217      1 course    0 view   view.php?id=1              1
 5 1441067607      2 110.142.152.217      1 course    0 view   view.php?id=1              1
 6 1441067617      2 110.142.152.217      1 course    0 view   view.php?id=1              1
 7 1441067646      2 110.142.152.217      1 course    0 view   view.php?id=1              1
 8 1441067681      2 110.142.152.217      1 course    0 view   view.php?id=1              1
 9 1441067774      2 110.142.152.217      1 course    0 view   view.php?id=1              1
10 1441069218      2 110.142.152.217      1 course    0 view   view.php?id=1              1
11 1441071815      2 110.142.152.217      1 course    0 view   view.php?id=1              1
12 1441071815      2 110.142.152.217      1 course    0 login  view.php?id=1              1
13 1441080275      2 110.142.152.217      1 user      0 view   view.php?id=0&course=1     2
14 1441080275      2 110.142.152.217      1 course    0 view   view.php?id=1              1
15 1441080275      2 110.142.152.217      1 course    0 view   view.php?id=1              1
16 1441082380      2 110.142.152.217      1 course    0 view   view.php?id=1              1
17 1441082494      2 110.142.152.217      1 course    0 view   view.php?id=1              1
18 1441082498      2 110.142.152.217      1 user      0 logout view.php?id=2&course=1     2
19 1441082504      2 110.142.152.217      1 user      0 login  view.php?id=0&course=1     2
20 1441082505      2 110.142.152.217      1 user      0 login  view.php?id=0&course=1     2
21 1441082508      2 110.142.152.217      1 user      0 login  view.php?id=0&course=2     2
22 1441082508      2 110.142.152.217      1 user      0 loam   view.DhD?id=0&course=1     2

我希望用户在每个网址属于不同课程的日子里获得最多查看网址。一个用户可以查看多个URL,但不能查看相同的课程。 我试过这个查询

SELECT count(DISTINCT `course`),`userid`,`course`,`module`,`url`, FROM_UNIXTIME(`time`,'%Y-%m-%d') as date FROM `mdl_log` where `time`>unix_timestamp('2016-06-01 00:00:00') AND `time`<unix_timestamp('2016-06-02 00:00:00') GROUP BY `course`

当我运行它时,它会限制用户使用相同的课程,但这也限制了其他用户。 例如,如果一个用户2次查看相同的课程,那么它会计算一个(这是正确的),但不计入用户查看过1次。意味着他们没有为其他用户获得相同的课程。请帮助我,我做错了什么..

1 个答案:

答案 0 :(得分:0)

因为您正在计算您要分组的列!这是完全错误的。

您应该算上DISTINCT USER_ID

SELECT count(DISTINCT `userid`),`userid`,`course`,`module`,`url`, FROM_UNIXTIME(`time`,'%Y-%m-%d') as date 
FROM `mdl_log` 
where `time`>unix_timestamp('2016-06-01 00:00:00') AND 
      `time`<unix_timestamp('2016-06-02 00:00:00')
GROUP BY `course`

我不明白你为什么选择userid?它将被随机选择,因为它不是group by子句的一部分。