MySQL: select Concat of strings and Length of result Concat

In my CREATE VIEW, I would like to:

SELECT CONCAT( t.str1, t.str2 ) AS Title, CHAR_LENGTH( Title ) AS Length 

but this disease causes an error:

Unknown header column in the field list.

What is the correct way to do this without repeating the same lines twice?

+2
source share
1 answer

You cannot reference the alias that you created in SELECT , use the expression instead:

 SELECT CONCAT( t.str1, t.str2 ) AS Title, CHAR_LENGTH(CONCAT( t.str1, t.str2 ) ) AS Length FROM table_name t 

You can use a subquery if you need:

 SELECT sub.Title, CHAR_LENGTH( sub.Title ) AS Length FROM ( SELECT CONCAT( t.str1, t.str2 ) AS Title FROM table_name t ) AS sub; 

All-at-once operation :

"All-at-Once operations" means that all expressions in the same logical logical sequence of queries are logically evaluated at the same time.

and

We cannot use an alias in the following column expression in a SELECT clause. In the request, we create the corrected first name, and we want to use it in the next column to get the full name, but the β€œAll at once” operation concept tells us that you cannot do this because all the expressions in the same phase are executed a logical query process (here SELECT) logically at the same time.

0
source

Source: https://habr.com/ru/post/1237507/


All Articles