计算VARCHAR字段中字符串出现的次数? [英] Count the number of occurrences of a string in a VARCHAR field?
本文介绍了计算VARCHAR字段中字符串出现的次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的表:
TITLE | DESCRIPTION
------------------------------------------------
test1 | value blah blah value
test2 | value test
test3 | test test test
test4 | valuevaluevaluevaluevalue
我试图弄清楚如何返回每个Description中字符串出现的次数.
I am trying to figure out how to return the number of times a string occurs in each of the DESCRIPTION's.
因此,如果我想计算值"出现的次数,则sql语句将返回以下内容:
So, if I want to count the number of times 'value' appears, the sql statement will return this:
TITLE | DESCRIPTION | COUNT
------------------------------------------------------------
test1 | value blah blah value | 2
test2 | value test | 1
test3 | test test test | 0
test4 | valuevaluevaluevaluevalue | 5
有没有办法做到这一点?我根本不想使用php,而只想使用mysql.
Is there any way to do this? I do not want to use php at all, just mysql.
推荐答案
这应该可以解决问题:
SELECT
title,
description,
ROUND (
(
LENGTH(description)
- LENGTH( REPLACE ( description, "value", "") )
) / LENGTH("value")
) AS count
FROM <table>
这篇关于计算VARCHAR字段中字符串出现的次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文