tags:

views:

273

answers:

5

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.

+1  A: 
SELECT  mo.*,
        (
        SELECT  COUNT(*)
        FROM    mytable mi
        WHERE   mi.name = mo.name
        )
FROM    mytable mo
Quassnoi
A: 
SELECT `name`,
    (
        SELECT COUNT(*)
        FROM `table` AS `alt`
        WHERE `alt`.`name` = `table`.`name`
    ) AS `num`
FROM `table`
chaos
+1  A: 

If you don't want to use a subquery, you can also join the table to itself like so: That would require a subquery. Perhaps something like this:

SELECT t1.name, COUNT(t2.name)
FROM my_table AS t1
INNER JOIN my_table AS t2 ON (t1.primary_key_field = t2.primary_key_field)
WHERE some_conditions
GROUP BY t1.name
VoteyDisciple
A: 

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
pjp
+1  A: 
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
Jess
Same as my answer - but I prefer my table naming :)
pjp
haha, yeah ... some_table and A/B aren't very descriptive ;)
Jess