Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: count grouped items?

Tags:

mysql

Basically, ok say my DB table has 2 columns: keyword and date.

I want to select rows but if multiple rows have the same keyword and date I want to group them and get a count of how many there are.

for example instead of getting this:

keyword: diamond, date: 20090601
keyword: diamond, date: 20090601
keyword: diamond, date: 20090602
keyword: diamond, date: 20090602
keyword: diamond, date: 20090602

I would get this:

keyword: diamond, date: 20090601, count: 2
keyword: diamond, date: 20090602, count: 3

Not sure how to do that?

Thanks!

like image 760
JD Isaacks Avatar asked Dec 30 '22 22:12

JD Isaacks


1 Answers

It's called the GROUP BY clause:

SELECT keyword, date, COUNT(1) AS count
FROM table
GROUP BY keyword, date
like image 162
Welbog Avatar answered Jan 02 '23 11:01

Welbog