0

i have MySQL DB, which stores data every minute (for date 1.7.2017 i have 1012 values of 1):

id| value |timeDate
1 | 1     |2017-07-01 ...
2 | 0     |2017-07-01 ...
3 | 1     |2017-07-01 ...
.....

I want to daily count values with value 1 (function):

$stmt = $db->prepare("SELECT day(dTimeDate), COUNT(*) FROM sensorData
        WHERE MONTH(dTimeDate) = :dateMonth AND YEAR(dTimeDate) = :dateYear AND _idSensor = :idSensor AND dValue = :valueSensor
        GROUP BY dTimeDate");
$stmt->execute(array(':dateMonth' => $m, ':dateYear' => $y, ':idSensor' => $idSensor, ':valueSensor' => $valueSensor));
$row2 =$stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($row2 as $row) {
    echo $row['day(dTimeDate)']. ' - ' .$row['COUNT(*)'].'<br/>';
}

I expect to get (like my SQL, example):

(day | Count)
1 - 1012 (count all values with value 1 in day 1)
2 - 105
...(31 lines, one for every day)

Instead i get:

(day | Count)
1 - 1
1 - 1
...(1012 lines of day 1 and value 1)
2 - 1

row2 returns array of all data instead of daily.

How can i get expected values?

4
  • Try to GROUP BY dTimeDate, value Commented Jul 30, 2017 at 12:39
  • I tried, get the same data. Commented Jul 30, 2017 at 12:54
  • I tried to reproduce your codes behaviour. I am not sure what you mean by your expected results. What are 1 and 2? The id or the value? Commented Jul 30, 2017 at 13:40
  • My select 'SELECT day(dTimeDate), COUNT(*) FROM sensorData' returns day and count values. That means 'day 1 - 1012(count values by day 1). 'row2' reurns array of all data (see update). Commented Jul 30, 2017 at 14:19

1 Answer 1

1

There is a little mistake in code:-

Change group by dTimeDate to group by day(dTimeDate)

Corrected complete code

$stmt = $db->prepare("SELECT day(dTimeDate), COUNT(*) FROM sensorData
    WHERE MONTH(dTimeDate) = :dateMonth AND YEAR(dTimeDate) = :dateYear AND _idSensor = :idSensor AND dValue = :valueSensor
    GROUP BY day(dTimeDate)");

$stmt->execute(array(':dateMonth' => $m, ':dateYear' => $y, ':idSensor' => $idSensor, ':valueSensor' => $valueSensor));
$row2 =$stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($row2 as $row) {
    echo $row['day(dTimeDate)']. ' - ' .$row['COUNT(*)'].'<br/>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for this obvious mistake, which take me allmost 2 days :)
It's OK, n Welcome. Just a Suggestion. When you are not able to find bugs, either do debugging line by line or leave the thing for few hours and then just review it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.