MySQL:选择字符串的Concat和结果Concat的长度 [英] MySQL: Select Concat of strings and Length of resulting Concat

查看:119
本文介绍了MySQL:选择字符串的Concat和结果Concat的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建视图"中,我想:

In my CREATE VIEW I would like to:

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

但这会产生错误:

字段列表"中的标题"列未知.

Unknown column 'Title' in 'field list'.

无需两次重复相同字符串的正确方法是什么?

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

推荐答案

您无法引用在SELECT中创建的别名,请改用表达式:

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 Operations" means that all expressions in the same logical query process phase are evaluated logically at the same time.

和:

我们不能在SELECT子句的下一列表达式中使用别名. 在查询中,我们创建一个更正的名字,并且我们想在其中使用它 下一列将产生全名,但会执行一次全操作 概念告诉我们您无法执行此操作,因为 评估相同的逻辑查询过程阶段(此处为SELECT) 在逻辑上同时.

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.

这篇关于MySQL:选择字符串的Concat和结果Concat的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆