朋友们,帮忙看下这问题!

sananiki 2009-06-06 04:30:23
首先看一下部分功能完成后显示的界面图:


然后再看下数据库的表:




我想大家也应该了解了我的意图,关键字“测试”在界面里出现了3次,而在表里它总共出现了5次
我现在所提出的问题是,如何把5个关键字“测试”都显示出来。

是不是要重新拆分字符串然后再统计?

已经用到的SQL语句,其作用是显示关键字左右两边的字.
SELECT substring(COMMENT,charindex('"+keyword+"',COMMENT)-"&leftmax&","&leftmax&") as leftlength,
substring(COMMENT,charindex('"+keyword+"',COMMENT)+len('"+keyword+"'),"&rightmax&") as rightlength
FROM WEBSITE where COMMENT like '%" & keyword & "%'
---"+keyword+","&leftmax&","&rightmax&"均为变量。



望哪位朋友能指点下怎样能让表里的5个“测试”以及左右字符都显示出来?
...全文
36 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lq20051610211 2009-06-07
  • 打赏
  • 举报
回复
UP~
xuejie09242 2009-06-06
  • 打赏
  • 举报
回复
类似楼上的做法,如果你要两个字段都显示,分别查这两个字段,再union一下就可以了。
ks_reny 2009-06-06
  • 打赏
  • 举报
回复

create table webite
(id int,
[content] varchar(2000))


insert webite
select 1 ,'一次测试' union
select 2,'几环' union
select 3,'一次测试,二次试,三次测试'

select a.id,substring([content], number, charindex(',', [content] + ',', number) - number) as col
into ttt
from webite a,master..spt_values b
where b.type='p'
and substring(',' + [content], number,1) = ','

create function getstr(@id int)
returns varchar(max)
as
begin
declare @s varchar(500)
select @s=isnull(@s+',','')+col from ttt
where id=@id and charindex('测试',col)>0
return @s
end

select distinct id,dbo.getstr(id) from ttt
where dbo.getstr(id) is not null
--------------------
1 一次测试
3 一次测试,三次测试
claro 2009-06-06
  • 打赏
  • 举报
回复
帮顶
ai_li7758521 2009-06-06
  • 打赏
  • 举报
回复
关注

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧