How to use aliases in queries using Aliases along with MySQL?

0

I would like to be able to use the 'AS' alias along with mysql without having to do query alignment, ie I want secondary queries to be named so that I can use them in the main query,

For example, in the case below the AS is used nested

  

Example 1 with queries inside queries (and this way works )

SELECT ID FROM (
    SELECT ID, msisdn FROM (
        SELECT * FROM TT2
    ) AS T
) AS T

The way I'd like to do would be without nesting, like this:

  

Example 2 with external queries against the main query (this way does not work )

SELECT name,LEFT(occupation,1) AS letra FROM OCCUPATIONS ORDER BY name AS tab1;

SELECT letra FROM tab1 ORDER BY letra ASC;
  

The main difference between examples 1 and 2 is that in the first case, queries are nested inside one another. The second query has sub queries outside the main query.

Form 2 does not work, nor does it use parentheses, how do to use a pseudo-name for the query to be represented temporarily with an " alias " in a non-nested way, is it possible?

    
asked by anonymous 01.12.2017 / 17:04

1 answer

1

Now with the editing of the question, it became clearer. In this case, you can use With :

With tab1 as (
SELECT name,LEFT(occupation,1) AS letra FROM OCCUPATIONS ORDER BY name
)

SELECT letra FROM tab1 ORDER BY letra ASC;

However, this clause is only available from version 8 of mysql. In earlier versions, queries should be nested.

    
01.12.2017 / 17:13