There are two commands commonly used in database querys and that are also indispensable in MySQL, GROUP BY and ORDER BY help you when you need sort and group data and now we will understand better how they work.
This command is used practically to sort a query from a column. This can be alphabetical ordering for columns of type varchar, for example, or numeric for columns of type integer. Using the following syntax you can use it.
SELECT [fiels] FROM [table] ORDER BY [Column]
As a practical example we sort a query the Products table.
SELECT Cod, Name
ORDER BY Cod
In the above query will return all records from the Products table sorted by the field that stores the code (Cod).
The command GROUP BY can be used as a kind of ordering, but its main function is to group the data so that it is possible to analyze the joint information of a particular block of information. For example, you can examine the records of a table by grouping them around town. Check the syntax of this command below.
SELECT [fields] FROM [table] GROUP BY [column]
Using the same example given above we group the data from a table of records from the city of each of the records.
SELECT Name, Phone
GROUP BY City
Thus will display the name and number of each record of the table records grouped by city registered.
Queries are essential in everyday life of any programmer, so to understand the functioning of these two commands is something essential. In addition, MySQL has become very common in environments for development, especially those using PHP. Qeer know more? Then click the link above to learn more about MySQL and PHP.