Mysql use 'as' in aliases or not?

Asked

Viewed 211 times

5

Writing a query in Mysql I came across this doubt, I made a search, however, I found nothing (or did not know how to look for), the doubt is simple, there is some difference between using or not the as when creating a temporary name for Mysql columns?

Example:

In the example below I used the as to define the temporary name:

SELECT nome_titulo as Titulo, nome_exibicao as 'Nome de Exibição' , created_at as 'Data de Criação' FROM moneyguard_database.titulos;

Already in this example I did not use just passed the temporary name aside:

SELECT nome_titulo Titulo, nome_exibicao 'Nome de Exibição' , created_at 'Data de Criação' FROM moneyguard_database.titulos;

In both cases I have the same result, is that there is some different performance or some case that one form is better than the other?

Thanks in advance for the clarification.

1 answer

10


According to the documentation of Mysql:

A select expression can receive an alias using AS alias_name. Alias is used as the name of the expression column and can be used in: ORDER BY, GROUP BY ou HAVING. For example:

SELECT CONCAT(last_name,', ',first_name) AS full_name   FROM mytable ORDER BY full_name;

The AS keyword is optional when creating an alias in an expression select with an identifier. The previous example could be written like this:

SELECT CONCAT(last_name,', ',first_name) full_name   FROM mytable
ORDER BY full_name;

However, as the AS is optional, a problem that can happen if you forget to put the comma between two select items: Mysql will interpret the second name as an alias. For example, in the following expression, columnb is treated as an alias:

SELECT columna columnb FROM mytable;

For this reason, it is good practice to use AS when specifying a column alias.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.