在搜索中,搜索词组用空格隔开,请问sql语句应该如何写?

天下第一刀2020 2009-04-15 05:57:14
非常像百度那样,搜索的词组使用空格隔开,实现并列查询的功能。请问词组应该如何分离出来呢?
...全文
258 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
youmijushi 2010-03-08
  • 打赏
  • 举报
回复
非常感谢,学习学习!
春天的气息 2009-04-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zzxap 的回复:]
C# code


STRING STRA=text1.text.trim.replace("'","''");
if (stra != "")
{
string[] strb=stra.split(" "); //把搜索条件用空格分开
//然后拼SQL
int inta=strb.length;
string sql=" select * from table where name like '"+ strb[0] +"' "
for (int i=1,i<inta,i++)
{
sql+=" or name like '"+ strb[i] +"' "

}
}
//然后执行SQL

不错,呵榀。

//你也可以拼union all,麻烦点
[/Quote]
wuyq11 2009-04-15
  • 打赏
  • 举报
回复
通过分词实现搜索
参考
gui0605 2009-04-15
  • 打赏
  • 举报
回复
以上答案可以实现你的功能。不过估计这样不是百度的写法......
teerhu 2009-04-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ralpha08 的回复:]
截取字符串

declare @conditions varchar(1000);
declare @condition varchar(1000);
declare @searchCondition varchar(1000);
set @conditions='北京 奥运 歌曲';
set @conditions=' '+@conditions+' ';
set @searchCondition='';
WHILE CHARINDEX(' ',@conditions)>0 BEGIN
SET @condition=SUBSTRING(@conditions,1,CHARINDEX(' ',@conditions));
SET @conditions=SUBSTRING(@conditions,CHARINDEX(' ',@…
[/Quote]
UP
helimin19 2009-04-15
  • 打赏
  • 举报
回复
string key = " 北京 奥运 歌曲 ";
string[] keys = Regex.Split(Regex.Replace(key.Trim(),"[\\s]{2,}"," "), " ");
string where = "";
foreach (string k in keys)
{
where += String.Format("title like '%{0}%' and ", k);
}
if (!String.IsNullOrEmpty(where)) where = where.Substring(0, where.Length - 4);
Response.Write(where);
kkun_3yue3 2009-04-15
  • 打赏
  • 举报
回复
下班了,回家再搞
ralpha08 2009-04-15
  • 打赏
  • 举报
回复
截取字符串

declare @conditions varchar(1000);
declare @condition varchar(1000);
declare @searchCondition varchar(1000);
set @conditions='北京 奥运 歌曲';
set @conditions=' '+@conditions+' ';
set @searchCondition='';
WHILE CHARINDEX(' ',@conditions)>0 BEGIN
SET @condition=SUBSTRING(@conditions,1,CHARINDEX(' ',@conditions));
SET @conditions=SUBSTRING(@conditions,CHARINDEX(' ',@conditions)+1,len(@conditions));

set @searchCondition=@searchCondition + ' like %'+@condition+'% ';
end
print @searchCondition
ws_hgo 2009-04-15
  • 打赏
  • 举报
回复
select * from tb where col=2 or col=1

用or
zzxap 2009-04-15
  • 打赏
  • 举报
回复
[code=C#]


STRING STRA=text1.text.trim.replace("'","''");
if (stra != "")
{
string[] strb=stra.split(" "); //把搜索条件用空格分开
//然后拼SQL
int inta=strb.length;
string sql=" select * from table where name like '"+ strb[0] +"' "
for (int i=1,i<inta,i++)
{
sql+=" or name like '"+ strb[i] +"' "

}
}
//然后执行SQL

//你也可以拼union all,麻烦点


[/CODE]
  • 打赏
  • 举报
回复
例如: 北京 奥运 歌曲
提炼为 北京、奥运、歌曲三个条件。分别 title like '%北京%' or title like ’%奥运%' or title like '%歌曲%'
kkun_3yue3 2009-04-15
  • 打赏
  • 举报
回复
传递SQL的时候是什么样子的?

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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