mysql查询查找重复最长时间的值

时间:2011-06-19 12:22:07

标签: mysql

我需要在b_id中找到重复最长时间的值。例如,下表的查询应该返回40(n.b。,查询应返回单个值)

| b_id | s_id | doi        | dos        | charge |
+------+------+------------+------------+--------+
|   10 |    3 | 0000-00-00 | 0000-00-00 |    200 |
|   10 |    2 | 0000-00-00 | 0000-00-00 |    200 |
|   20 |    1 | 0000-00-00 | 0000-00-00 |    200 |
|   30 |    2 | 0000-00-00 | 0000-00-00 |    200 |
|   40 |    4 | 0000-00-00 | 0000-00-00 |    200 |
|   40 |    5 | 0000-00-00 | 0000-00-00 |    200 |
|   70 |    5 | 0000-00-00 | 0000-00-00 |    200 |
|   40 |    4 | 0000-00-00 | 0000-00-00 |    200 |

1 个答案:

答案 0 :(得分:2)

SELECT TOP 1 COUNT(*) as total FROM dbo.Table GROUP BY b_id ORDER BY total DESC

编辑: ups,mysql等价物:

SELECT COUNT(*) as total FROM dbo.Table GROUP BY b_id ORDER BY total DESC LIMIT 1

EDIT2: 评论之后:

SELECT b_id FROM dbo.Table GROUP BY b_id ORDER BY COUNT(b_id) DESC LIMIT 1