如何将所有字数统计为mysql

时间:2014-02-20 12:36:05

标签: php mysql

我已经知道如何计算每一行中的单词:

SELECT *,
IF(words <> '', (length(words)-length(replace(words,' ',''))+1), 0) AS words_count
FROM table

但现在我需要从所有行中计算所有计数。

我该怎么做?

谢谢大家。

1 个答案:

答案 0 :(得分:4)

如果您想要总和,请使用聚合查询:

SELECT sum(if(words <> '', (length(words)-length(replace(words,' ',''))+1), 0)) AS words_count
FROM table;

编辑:

如果没有行,可以这样做以避免NULL值:

SELECT coalesce(sum(if(words <> '', (length(words)-length(replace(words,' ',''))+1), 0)), 0) AS words_count
FROM table;