简体   繁体   中英

mysql distinct and count

I have table like this:

+------+-------+
| user | data  |
+------+-------+
|  1   |   a   |
+------+-------+
|  1   |   b   |
+------+-------+
|  2   |   c   |
+------+-------+

I need to get something like:

Array
(
    [1] => 2
    [2] => 1
)

Get unique user and count the instances in data row, any ideas?

I've tried with this query:

SELECT DISTINCT user from table ORDER BY ID DESC

You should use GROUP BY for this.

SELECT `User`, COUNT(data) TotalCount
FROM tableName
GROUP BY `User`
ORDER BY TotalCount DESC

SELECT COUNT(*) from table GROUP BY user

(not tested)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM