In this tutorial I will show you
how to use Group By Function in MySql.
Group by is very powerful function which can help you to get an result from
MySql database in very specific view, format and order. Later resulted data in this format, can be parsed for some other operations or manipulations.
In our example we will use freeware and web based
PhpMyAdmin for database administration and manipulation.
In this tutorial we will use table with "IP-to-Country" data. This is a list with IP range zones of most countries, with their respective names and TLD suffixes.
In other words the tables looks in this way:
But this result is displayed with no any order and we can improve our query to use Order by function to display data from `tld` column sorted alphabetically like this:
Like you can see, from the first select result in this tutorial, there is more than one row for UNITED STATES country with `tld`=US. But when we use Group by function we take only one row, and in this row exist only one value for `ip_from` and `ip_to`.
For this, you have to note, that when use Group by function you can not be sure which row will be returned sorting with Order by function.