MySQL: Select Concat of strings and Length of resulting Concat

前端 未结 1 1283
我寻月下人不归
我寻月下人不归 2021-01-23 01:04

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:

<
相关标签:
1条回答
  • 2021-01-23 01:41

    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.

    0 讨论(0)
提交回复
热议问题