MySQL:选择字符串的Concat和结果Concat的长度 [英] MySQL: Select Concat of strings and Length of resulting Concat
本文介绍了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;
查看全文