MySQL: To exclude some results based on specified conditions -
i stuck @ problem. problem has 2 tables, namely hackers , challenges. here schema:
hackers(hacker_id: int, name string) challenges(challenge_id: int, hacker_id int)
i trying write query print hacker_id, name, , total number of challenges created each student. sort results total number of challenges in descending order. if more 1 student created same number of challenges, sort result hacker_id. if more 1 student created same number of challenges , count less maximum number of challenges created, exclude students result.
here query:
select hackers.hacker_id , hackers.name , count(challenges.challenge_id) challenges_created hackers left join challenges on hackers.hacker_id = challenges.hacker_id having count(challenges_created) >= max(challenges_created) order challenges_created desc, hackers.hacker_id asc
i getting wrong output! please tell me getting wrong!
sample input :
hackers table:image challengers table :image
sample output: 21283 angela 6 88255 patrick 5 96196 lisa 1
this not real answer, start point you:
http://sqlfiddle.com/#!9/6910c/5
select h.hacker_id , h.name , count(challenges.challenge_id) challenges_created hackers h left join challenges on h.hacker_id = challenges.hacker_id group h.hacker_id #having challenges_created >= max(challenges_created) order challenges_created desc, h.hacker_id asc
i've commented out having
clause, because can not understand trying achieve.
explain please why expect 3 rows in result? why rose
, frank
should excluded result set?
Comments
Post a Comment