SQL中,如何搜索出包含有指定字符的数据?

来源:百度知道 编辑:UC知道 时间:2024/05/21 08:12:51
比如我去搜索“China”,“中华人民共和国”,“CN”,“大陆”等的关键词时就等同于我去搜索“中国”....表如下:

Keyword | Keywordlike
中国 | China,中华人民共和国,CN,大陆

请问语句应该是如何写,是用contains麽?Keywordlike的分隔符应该是什么?我是使用VisualStudio2008的
不能使用like,因为如果直接搜索“中”,也会出现中国。我要的是搜索“Chi”,“中”,“中华人民”,“C”都不能搜索出中国,只有完全符合Keywordlike里面的,才可以搜索出“中国”..

为什么不把数据存储成这样呢?
PKID | Keyword | Keywordlike
1 | 中国 | China
2 | 中国 | 中华人民共和国
3 | 中国 | CN
4 | 中国 | 大陆

如果非要像你所说的,也可以,但程序会太死
select Keyword = case when Keywordlike ='China'or Keywordlike ='中华人民共和国' or Keywordlike ='CN' or Keywordlike ='大陆 ' then '中国' end

-----------------
以下是我的测试语句
declare @Keyword varchar(100)
declare @Keywordlike varchar(100)
set @Keywordlike = '大陆'
select @Keyword = case when @Keywordlike ='China'or
@Keywordlike ='中华人民共和国' or @Keywordlike ='CN' or @Keywordlike ='大陆 ' then '中国' end
select @Keyword

select * from keyword where Keywordlike like '%搜索的内容%'
这样就找出keyword了,再去搜索keyword相关的内容,就OK了。Keywordlike分隔符用“,”“|”都是可以的。

那你用“,”和“|”等特殊字符来分隔都是可以的。可以用CHARINDEX和SUBstring函数来拆分。charindex(Keywordlike,‘,’,0)。在sql里不是很好处理 ,取出来再程序里要好办一些。

SELECT *
FROM keyword
WH