简体   繁体   中英

MySQL: Select Concat of strings and Length of resulting Concat

In my CREATE VIEW I would like to:

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

but this ill produce error:

Unknown column 'Title' in 'field list'.

What is the correct way of doing this without having to Concat same strings twice?

You cannot refer the alias you created in SELECT , use 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 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 query process phase are evaluated logically at the same time.

and:

We cannot use an alias in next column expression in the SELECT clause. In the query we create a corrected first name and we want to use it in next column to produce the full name, but the All-at-Once operations concept tells us you cannot do this because all expressions in the same logical query process phase (here is SELECT) are evaluated logically at the same time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM