I am new to sql, and I would like to create a query that will count all my article id's for each day. but the problem the date column is also containing time.. so how I can make the query to group by just by date without time?
for example:
id|article_id|date (timestamp)
1|22|2014-01-10 13:30:10
1|23|2014-02-10 12:30:10
Thanks Shai
Try this:
select count(article_id) from YourTable group by date(date)
Try this:
select date(date), count(article_id) from YourTable group by date(date);
I am too late in the race. Hope it may help some one in future.
There are 2 solution's for this
You can take DATE as filter option on your date timestamp in GROUP BY as follows
SELECT COUNT(id) as article_count, DATE(date) GROUP BY DATE(date);
You can take the alias of the SELECT column with is filtered by DATE() in GROUP BY
SELECT COUNT(id) as article_count, DATE(date) published_date GROUP BY published_date;
Hope it helped. Happy Coding!
.
$dayCount = 0;
$previousDay = "";
$query = $database->prepare('
SELECT DATE_FORMAT(date, '%d/%m/%Y') AS date FROM my_table ORDER BY date');
$query->execute();
while ($output = $query->fetch()) {
$day = $output['date'];
if($previousDay != $day)
{ $dayCount ++; }
else {
echo $dayCount;
$dayCount = 0;
}
$previousDay = $day;
}
Can can find DATE_FORMAT output here:
http://dev.mysql.com/doc/refman/5.0/fr/date-and-time-functions.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With