简体   繁体   English

找出值在mysql中两个日期之间的频率

[英]Find out how often a value is between 2 dates in mysql

Hi with the follow code I request what dates are all in my database without duplicates. 嗨,下面的代码要求我数据库中的所有日期都没有重复。 Then I save it to an array. 然后我将其保存到数组。 In the array I also need an other value. 在数组中,我还需要另一个值。

The value I need is how much users are in one day in the database without duplicates. 我需要的价值是一天之内数据库中有多少用户,且没有重复。 For Example the array must later lookslike 23.07.2013 - 10, 24.07.2013 - 50 (users). 例如,该数组以后必须看起来像23.07.2013-10,24.07.2013-50(用户)。 I search for several hours but I don't find a good mysql query. 我搜索了几个小时,但找不到很好的mysql查询。

$query = "SELECT id, user, timestamp FROM stat WHERE timestamp BETWEEN '$datum1' AND '$datum2' GROUP BY timestamp";
$result = mysql_query($query,$db);

while($row = mysql_fetch_assoc($result))
    {
        mysql_num_rows($result);
        $dataset1[] = array(strtotime($row['timestamp']),$number_of_users_on_this_day);
    }

Try: 尝试:

$query = "SELECT id, user, COUNT(*) as count FROM stat WHERE timestamp BETWEEN '$datum1' AND '$datum2' GROUP BY timestamp"; $ query =“ SELECT id,user,COUNT(*)作为从“ $ datum1”和“ $ datum2” GROUP BY时间戳之间的统计时间戳记开始的计数”;

This will return the number of entries in the value 'count' 这将返回值“ count”中的条目数

if you want distinct data, in place of * use 如果您需要不同的数据,请使用*代替

COUNT(DISTINCT id) COUNT(DISTINCT ID)

with whatever field you want to be unique in place of 'id' 使用任何您想唯一代替“ id”的字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM