如何在同一语句中使用别名?请参阅以下语句中的body
别名。当我在总和(长度(身体)中使用它时,我得到unknown column
。
SELECT substring_index(userBody, ' ', 60) as body, SUM(LENGTH(body)-LENGTH(REPLACE(body,' ',''))+1) as wordCount
from articles where artcId = 10;
答案 0 :(得分:3)
您不能在下一个选择列中使用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;
答案 1 :(得分:0)
您不能使用别名列名来分离其他列的数据。
您可以将子查询或使用相同的功能用于其他列,如下所述:
使用子查询
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;
答案 2 :(得分:0)
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