我已经知道如何计算每一行中的单词:
SELECT *,
IF(words <> '', (length(words)-length(replace(words,' ',''))+1), 0) AS words_count
FROM table
但现在我需要从所有行中计算所有计数。
我该怎么做?
谢谢大家。
答案 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;