mysql中怎么统计某字段里某个字符的个数

mysql中怎么统计某字段里某个字符的个数,第1张

CREATE  function [dbo].[GetCharIndexNum](@findstring varchar(max),@string varchar(max))

returns int

AS

BEGIN

   declare @location int , --要找字符位置

           @num int  --要找的字符出现的次数

   

   set @num =0

   set @location = charindex (@findstring,@string)

   while @location >0  ---字符串中存在要找的字符

     begin

      set @num =@num +1

      set @string =substring(@string,@location+1,len(@string))

      set @location = charindex (@findstring,@string)

    end

return @num

END

--举个例子调用这个标量值函数 select   [dbo].[GetCharIndexNum]('5','abc5ab5')

返回值2,5这个字符出现了2次

select * from post order by length(content) desc limit 1

这样每次取速度太慢,可以通过before insert的触发器统计数字,然后通过数字索引查询。

// assume field lenofcontent exists in table post

select * from post order by lenofcontent desc limit 1


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/8435680.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-16
下一篇 2023-04-16

发表评论

登录后才能评论

评论列表(0条)

保存