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.
MySQL COUNT() Function The COUNT() function returns the number of records returned by a select query.
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.
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.
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 .
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
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
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