MySQL: Another max per group? Two tables -
my database tracks sections users have completed:
table 'users':
id user_id sections_id // 4 46 1 5 46 2 6 46 4 7 46 5 //
table 'sections':
id header_id name 1 1 1/3 2 1 2/3 3 1 3/3 4 2 1/3 5 2 2/3 6 2 3/3
the following query
select a.sections_id ,b.header_id users join sections b on a.sections_id = b.id a.user_id = 46; // a.user_id can user_id, added clarity
gives me:
sections_id header_id 1 1 2 1 4 2 5 2
what want max section id per header particular user, know section need serve user:
sections_id header_id 2 1 5 2
i'm assuming max per group problem, can't quite head around solution. throw data php , parse out there, seems should able via sql. tia!
this simple group by
query:
select s.header_id, max(u.sections_id) users u join sections s on u.sections_id = s.id u.user_id = 46 group s.header_id;
i changed aliases initials of table. makes query easier follow.
edit: sqlfiddle here: http://sqlfiddle.com/#!2/dbb5a/2
Comments
Post a Comment