如何在同一语句中使用别名?请参阅body
下面语句中的别名。当我在 sum(length(body) 中使用它时,我得到unknown column
.
SELECT substring_index(userBody, ' ', 60) as body, SUM(LENGTH(body)-LENGTH(REPLACE(body,' ',''))+1) as wordCount
from articles where artcId = 10;
您不能body
在下一个选择列中使用别名,最好在以下部分中使用相同的“别名”块。有关更多说明,请参阅以下查询。
SELECT
substring_index(userBody, ' ', 60) AS body,
SUM(
LENGTH(substring_index(userBody, ' ', 60)) - LENGTH(REPLACE(substring_index(userBody, ' ', 60),' ',''))+1
) AS wordCount
FROM articles
WHERE artcId = 10;
您不能使用别名列名来提取另一列的数据。
您可以使用子查询或对另一列使用相同的函数,如下所述:
使用子查询
SELECT body, SUM(LENGTH(body)-LENGTH(REPLACE(body,' ',''))+1) AS wordCount
FROM (SELECT SUBSTRING_INDEX(userBody, ' ', 60) AS body
FROM articles WHERE artcId = 10) AS A
或使用相同的功能
SELECT SUBSTRING_INDEX(userBody, ' ', 60) AS body,
SUM(LENGTH(SUBSTRING_INDEX(userBody, ' ', 60))-LENGTH(REPLACE(SUBSTRING_INDEX(userBody, ' ', 60),' ',''))+1) AS wordCount
FROM articles WHERE artcId = 10;
你不能。或者,您可以使用子查询,例如。
SELECT body,
SUM(LENGTH(body)-LENGTH(REPLACE(body,' ',''))+1) as wordCount
FROM
(
SELECT substring_index(userBody, ' ', 60) as body
from articles
where artcId = 10
) s