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.

ORDER BY

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

FROM product

ORDER BY Cod

In the above query will return all records from the Products table sorted by the field that stores the code (Cod).

GROUP BY

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

FROM Register

GROUP BY City

Thus will display the name and number of each record of the table records grouped by city registered.

mysql

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.

You might also like…

PROCESAMIENTO AJAX EN SCRIPTCASE

Ajax en los formularios de Scriptcase puede usarse para recargar un campo de tipo de select basado e...

IMPORTAR IMAGENES DE LA BASE DE DATOS Y UTILIZAR CLASES DE BOOSTRAP

Como muchas personas ya saben, Bootstrap es una biblioteca de front-end libre y de código abierto p...

TENDENCIAS DE DESARROLLO WEB PARA 2017

Usted verá en este post tendencias para 2017 en Diseño Web, en el medio digital y en el desarrollo...

Comment this post

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at sales@scriptcase.net. Learn more about our Privacy Police.