Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need some help with counting both unique and duplicate values in MySQL. I want to know how many records there are total, and also how many is there two times and three times and so on...

Do I need to use UNION or something? I think SUM would be the best solution for me because of I might use some joins with this in future.

Sample data:

| custId | name   |
|--------|--------|
| 1001   | Alex   |
| 1001   | Alex   |
| 1002   | Daniel |
| 1003   | Mark   |
| 1002   | Daniel |

Sample results:

| total | twoTimes | threeTimes |
|-------|----------|------------|
|     3 |        2 |          0 |

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
195 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just a basic group by should do it

    SELECT YourValue, Count(YourValue)
    FROM YourTable
    GROUP BY YourValue

If you want only a category, like unique values ADD

   HAVING Count(YourValue)  = 1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...