Just use SQL group by function.
Few examples from MySQL website
In reply to Re: Perl/SQL query - grouping 'one to many' records
by ahmad
in thread Perl/SQL query - grouping 'one to many' records
by pcdj_d
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |