MySQL: Count entries without grouping?

前端 未结 6 986
予麋鹿
予麋鹿 2021-02-07 06:32

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 J         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-07 07:06

    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
    

提交回复
热议问题