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 sid,case score when >=60 then '及格'

else '不及格' end as 是否及格 from sc

-- 将地址里的'东三区',替换成'西二区'

update t1 set add=replace(add,'东三区','西二区')

-- 列出姓张的同学资料

select * from students where sname like '张%'

用全文索引,把字段的索引设为fulltext

select count(*) from table where MATCH(field1) AGAINST('aaa')

select count(*) from table where MATCH(field1) AGAINST('bbb')

select count(*) from table where MATCH(field1) AGAINST('ccc')

select count(*) from table where MATCH(field1) AGAINST('ddd')

目前只能想到分别查询了


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存