count without group

前端 未结 6 2041
眼角桃花
眼角桃花 2020-12-14 01:19

I have one table named GUYS(ID,NAME,PHONE) and i need to add a count of how many guys have the same name and at the same time show all of them so i can\'t group them. exampl

6条回答
  •  醉梦人生
    2020-12-14 01:44

    You can still use a GROUP BY for the count, you just need to JOIN it back to your original table to get all the records, like this:

    select g.ID, g.Name, g.Phone, gc.Count
    from Guys g
    inner join (
        select Name, count(*) as Count
        from Guys
        group by Name
    ) gc on g.Name = gc.Name
    

提交回复
热议问题