简体   繁体   中英

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). I search for several hours but I don't find a good mysql query.

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

This will return the number of entries in the value 'count'

if you want distinct data, in place of * use

COUNT(DISTINCT id)

with whatever field you want to be unique in place of 'id'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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