Mysql Tips #2 : Using Group

Using group in MySQL:

Group by is being used to group field into a single row to perform count, sum avg etc.

SELECT *
FROM `test_results` 
WHERE parent_code =11
GROUP BY username

Using Group by with MAX

SELECT MAX( points ) , username
FROM `test_results` 
WHERE parent_code =11
GROUP BY username

Similar can be used for min, avg, count…etc

Using Group by with Having

select gid
 from `gd`
 group by gid
 having count(*) > 10
order by lastupdated desc

Happy coding!!

….Sometime simple tip makes someone’s coding life easier…………