mysql query to find value which repeats for maximum time
I need to find value in b_id
which repeats for maximum time. for example query for the table below should return 40 (n.b., query should return a single value)
| b_id | s_id | doi | dos | charge | +------+------+------------+------------+--------+ | 10 | 3 | 开发者_JS百科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 |
SELECT TOP 1 COUNT(*) as total FROM dbo.Table GROUP BY b_id ORDER BY total DESC
edit: ups, mysql equivalent:
SELECT COUNT(*) as total FROM dbo.Table GROUP BY b_id ORDER BY total DESC LIMIT 1
edit2: after the comment:
SELECT b_id FROM dbo.Table GROUP BY b_id ORDER BY COUNT(b_id) DESC LIMIT 1
精彩评论