如何在同一個語句中使用別名?在下面的語句中查看body
別名。當我和(長(機構)使用它,我得到unknown column
。在同一個sql語句中使用mysql別名
SELECT substring_index(userBody, ' ', 60) as body, SUM(LENGTH(body)-LENGTH(REPLACE(body,' ',''))+1) as wordCount
from articles where artcId = 10;
如何在同一個語句中使用別名?在下面的語句中查看body
別名。當我和(長(機構)使用它,我得到unknown column
。在同一個sql語句中使用mysql別名
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;
不能使用別名列名detch數據anothe列。
您可以使用子查詢或採用S對於另一列AME功能如下解釋:
使用子查詢
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