nifu nifu - 3 years ago 138
MySQL Question

MySQL Count in select

i have a MySQL table with the username and bought items. Likeā€¦

Username1 - Item_A
Username1 - Item_A
Username2 - Item_C
Username2 - Item_D

I need to count how many differed Itemy every user has bought.


Username1 - 1
Username2 - 2

Does anyone have an idea? Thank you very much in advance.

Answer Source

You can group by the user and apply a distinct count to the items:

SELECT   username, COUNT(DISTINCT item)
FROM     mytable
GROUP BY username
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download