Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql group by date without time

Tags:

sql

mysql

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

like image 898
Shai Avatar asked Sep 29 '14 09:09

Shai


4 Answers

Try this:

select count(article_id) from YourTable group by date(date)
like image 90
Ronak Shah Avatar answered Sep 16 '22 12:09

Ronak Shah


Try this:

select date(date), count(article_id) from YourTable group by date(date);
like image 31
Nestor Avatar answered Sep 20 '22 12:09

Nestor


I am too late in the race. Hope it may help some one in future.

There are 2 solution's for this

  1. 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);

  2. 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!

.

like image 20
Channaveer Hakari Avatar answered Sep 16 '22 12:09

Channaveer Hakari


$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

like image 20
gr3g Avatar answered Sep 16 '22 12:09

gr3g