使用SQL计算列中的单词

时间:2012-02-03 11:35:38

标签: php mysql sql doctrine

id | words                            | other
1  | lorem ipsum                      | dsf
2  | Lorem Ipsum is simply dummy text | sads
3  | simply dummy text                | sdf
4  | Lorem Ipsum  text                | sdfsdf
5  | simply dummy Lorem               | asddasd 

我做SELECT * FROM单词。 我如何计算这个查询答案中有多少单词?在这个例子应该是17。 我使用PHP和Doctrine。

2 个答案:

答案 0 :(得分:5)

SELECT LENGTH(words) - LENGTH(REPLACE(words, ' ', '')) + 1 AS words_count
FROM table_name

答案 1 :(得分:0)

SELECT SUM( LENGTH(words) - LENGTH(REPLACE(words, ' ', ''))+1)
FROM table