本文介紹了計算字符串在 VARCHAR 字段中出現(xiàn)的次數(shù)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我有一張這樣的桌子:
TITLE | DESCRIPTION
------------------------------------------------
test1 | value blah blah value
test2 | value test
test3 | test test test
test4 | valuevaluevaluevaluevalue
我想弄清楚如何返回字符串在每個描述中出現(xiàn)的次數(shù).
I am trying to figure out how to return the number of times a string occurs in each of the DESCRIPTION's.
所以,如果我想統(tǒng)計'value'出現(xiàn)的次數(shù),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
有沒有辦法做到這一點(diǎn)?我根本不想用php,就用mysql.
Is there any way to do this? I do not want to use php at all, just mysql.
推薦答案
這應(yīng)該可以解決問題:
SELECT
title,
description,
ROUND (
(
LENGTH(description)
- LENGTH( REPLACE ( description, "value", "") )
) / LENGTH("value")
) AS count
FROM <table>
這篇關(guān)于計算字符串在 VARCHAR 字段中出現(xiàn)的次數(shù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!