mysql与计数有关

时间:2012-05-19 01:50:59

标签: php mysql sql count

我有这样的表:

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

我需要得到类似的东西:

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

获取唯一身份用户并计算数据行中的实例,任何想法?

我尝试过这个查询:

SELECT DISTINCT user from table ORDER BY ID DESC

2 个答案:

答案 0 :(得分:6)

您应该使用GROUP BY

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

答案 1 :(得分:0)

从表中选择COUNT(*) GROUP BY用户

(未经测试)