Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Count entries without grouping?

Tags:

sql

mysql

I want to pull results and count how many of each name is pulled but without grouping...

for example I want this:

John Doe 3
John Doe 3
John Doe 3
Mary Jane 2
Mary Jane 2

instead of this:

John Doe 3
Mary Jane 2

Does that make sense?

Thanks.

like image 335
JD Isaacks Avatar asked Aug 11 '09 15:08

JD Isaacks


People also ask

How do I count number of entries in MySQL?

MySQL COUNT() Function The COUNT() function returns the number of records returned by a select query.

Is GROUP BY necessary for count?

COUNT() with GROUP byThe use of COUNT() function in conjunction with GROUP BY is useful for characterizing our data under various groupings. A combination of same values (on a column) will be treated as an individual group.

How do I count the number of rows in a MySQL GROUP BY?

In MySQL, the COUNT() function calculates the number of results from a table when executing a SELECT statement. It does not contain NULL values. The function returns a BIGINT value. It can count all the matched rows or only rows that match the specified conditions.

What is Rowcount in MySQL?

rowcount. This read-only property returns the number of rows returned for SELECT statements, or the number of rows affected by DML statements such as INSERT or UPDATE .


2 Answers

SELECT b.name, a.the_count
FROM
  some_table b, 
  (SELECT name, COUNT(*) AS the_count
  FROM some_table
  GROUP BY name) AS a
WHERE b.name = a.name
like image 167
Beep beep Avatar answered Nov 01 '22 14:11

Beep beep


This uses group by but gets the output in the format you want.

SELECT Name, NG.NameCount
FROM Names
INNER JOIN
    (SELECT Name, Count(1) As NameCount
    FROM Names
    GROUP BY Name) NG
ON Names.Name = NG.Name
like image 24
pjp Avatar answered Nov 01 '22 14:11

pjp