Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL count and group by day

Tags:

sql

mysql

I have the following structure

ID    DATE(DATETIME)         TID
1     2012-04-01 23:23:23    8882

I'm trying to count the amount of rows and group them by each day of the month that matches TID = 8882

Thanks

like image 902
user838437 Avatar asked May 01 '12 08:05

user838437


People also ask

Does Count work with GROUP BY?

The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.

How do I count groups in MySQL?

SELECT DISTINCT ColumnName FROM TableName; Using the COUNT() function with the GROUP BY clause, then the query will produce the number of values as the count for each subgroup created based on the table column values or expressions.

How do I count rows in a GROUP BY?

To count the number of rows, use the id column which stores unique values (in our example we use COUNT(id) ). Next, use the GROUP BY clause to group records according to columns (the GROUP BY category above). After using GROUP BY to filter records with aggregate functions like COUNT, use the HAVING clause.

Does Count work without GROUP BY?

Using COUNT, without GROUP BY clause will return a total count of a number of rows present in the table. Adding GROUP BY, we can COUNT total occurrences for each unique value present in the column. we can use the following command to create a database called geeks.


2 Answers

You can group using the DAY function:

SELECT DAY(Date), COUNT(*)
FROM table
WHERE TID = 8882
GROUP BY DAY(Date)
like image 151
alexn Avatar answered Oct 02 '22 11:10

alexn


Not sure exactly what you mean by day of the month -- do you want to group the 1st of Feb with the 1st of March? Or do you just mean date? Assuming the latter, how about this:

SELECT DATE(date) as d,count(ID) from TABLENAME where TID=8882 GROUP by d;
like image 40
Ben Avatar answered Oct 02 '22 12:10

Ben