简体   繁体   English

MySQL:选择字符串的连接和结果连接的长度

[英]MySQL: Select Concat of strings and Length of resulting Concat

In my CREATE VIEW I would like to:在我的 CREATE VIEW 中,我想:

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创建的别名,请改用表达式:

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 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.我们不能在 SELECT 子句的下一列表达式中使用别名。 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.在查询中,我们创建了一个更正的名字,并希望在下一列中使用它来生成全名,但是一次性操作概念告诉我们您不能这样做,因为同一逻辑查询过程阶段中的所有表达式(这里是 SELECT)同时进行逻辑评估。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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