defroster Posted April 18, 2013 Share Posted April 18, 2013 Hello I have a table over videos submitted by people: id / by_person / type / title / ----------------------------------------- 1 | 3 | 1 | title1 | 2 | 4 | 1 | title2 | 3 | 3 | 1 | title3 | 4 | 4 | 2 | title4 | 5 | 3 | 1 | title5 | 6 | 6 | 2 | title6 | 7 | 6 | 2 | title7 | 8 | 4 | 2 | title8 | 9 | 3 | 1 | title9 | 10 | 4 | 1 | title10 | 11 | 4 | 1 | title11 | 12| 3 | 1 | title12 | How do I SELECT the top 2 people who have submitted the most videos with type=1 ? So I get a presentation like this: 1. Person(3) - 5 videos 2. Person(4) - 3 videos Thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted April 18, 2013 Share Posted April 18, 2013 SELECT by_person, COUNT(*) as total FROM videos ORDER BY total DESC LIMIT 2 Quote Link to comment Share on other sites More sharing options...
defroster Posted April 18, 2013 Author Share Posted April 18, 2013 Thanks Barand, but I would only like to select the count where type=1, how do I bake that in? Quote Link to comment Share on other sites More sharing options...
Solution defroster Posted April 18, 2013 Author Solution Share Posted April 18, 2013 Thanks, solved now SELECT by_person, count(*) AS total FROM videos WHERE type = 1 GROUP BY by_person ORDER BY total DESC LIMIT 2 DEMO: http://sqlfiddle.com/#!2/b2916/22 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.