How to use pseudo names with CONCAT and MySQL?

1

A question about MySQL asks for the output to come out in that response format.

When I finish my solution through a query in MySQL , I encounter the following problem.

Below is a first example solution of my query that Works

SELECT CONCAT('There are a total of ' , COUNT(name),' ', occupation ) FROM OCCUPATIONS GROUP BY occupation ORDER BY COUNT(name) ASC ;

Below is a second example of my query that does not work

  SELECT CONCAT ('There are a total of', COUNT (name) AS numb , '', occupation) FROM OCCUPATIONS GROUP BY occupation ORDER BY numb ASC; '

The problem in question is that I would like to rename COUNT(name) to numb through a aliase 'or a pseudo name, but I come across this error.

ERROR 1583 (42000) at line 3: Incorrect parameters in the call to native function 'concat'

Would it be possible to circumvent this problem, ie is it possible to use pseudo names within the function CONCAT ?

    
asked by anonymous 02.12.2017 / 20:53

2 answers

1

According to the standard it is not possible to use aliases in the same SELECT statement in which they are created. One solution is to use a sub-query.

The standard informs that the execution sequence of the instructions is as follows:

1. FROM 
2. WHERE
3. GROUP BY
4. HAVING
5. SELECT
6. ORDER BY
7. TOP

This explains, for example, why you can use an alias in the ORDER BY clause.

    
02.12.2017 / 21:00
1

It is not possible, and in this case it does not even make sense. I imagined initially that I would use the data for something else, as it would not be completely unnecessary to name the result of the expression used in the concatenation.

It does not make sense not to want repetition because the syntax asks for repetition.

    
02.12.2017 / 20:56