使用拆分搜索字符串的过程

时间:2015-11-13 09:47:40

标签: sql sql-server string stored-procedures split

我需要使用OR条件在多个列中搜索字符串&还希望通过SPACE拆分String。防爆。如果用户搜索'软件开发人员'那么一个简单的查询就像

Select * from TableName Where Col1 LIKE '%Software%' OR Col2 LIKE '%Software%' OR Col3 LIKE '%Software%' OR Col1 LIKE '%Developer%' OR Col2 LIKE '%Developer%' OR Col3 LIKE '%Developer%'

在程序中应该是一个参数。像

这样的东西
ProcedureName 'Software Developer'

我可以在上面的Query上工作,但我需要知道,我能做的是否有简单,快速和更好的查询。

3 个答案:

答案 0 :(得分:0)

你瘦了,你可以使用像

这样的查询
  

从表中选择*其中(col1 + col2 + col3喜欢'%Software%')或者(col1 + col2 + col3喜欢'%Engineer%')

答案 1 :(得分:0)

SELECT     dbo.JD_tblJob.EmployerID, dbo.JD_tblJob.PostDate, dbo.JD_tblJob.JobID, dbo.JD_tblJob.JobTitle, dbo.JD_tblJob.JobDescr, dbo.JD_tblJob.MinWorkExp, 
                  dbo.JD_tblJob.MaxWorkExp, dbo.JD_tblJob.MinSalary, dbo.JD_tblJob.MaxSalary, dbo.JD_tblJob.HideSalary, dbo.JD_tblJob.NoOfVacancies, 
                  dbo.JD_tblJob.JobLocation, dbo.JD_tblJob.Active, dbo.JD_tblEmployer.CompanyName, tblKeySkill.fldName AS KeySkillName, 
                  tblFuncArea.fldName AS FuncAreaName FROM dbo.JD_tblJob INNER JOIN dbo.JD_tblMaster AS tblFuncArea ON dbo.JD_tblJob.FuncAreaID = tblFuncArea.ID INNER JOIN dbo.JD_tblMaster AS tblKeySkill ON dbo.JD_tblJob.KeySkillID = tblKeySkill.ID INNER JOIN dbo.JD_tblEmployer ON dbo.JD_tblJob.EmployerID = dbo.JD_tblEmployer.ID WHERE (dbo.JD_tblJob.JobTitle LIKE '%Engineer%') OR (tblKeySkill.fldName LIKE '%Engineer%') OR (tblFuncArea.fldName LIKE '%Engineer%') OR (dbo.JD_tblJob.JobTitle LIKE '%Software%') OR (tblKeySkill.fldName LIKE '%Software%') OR (tblFuncArea.fldName LIKE '%Software%')

它给了我结果,但下面没有显示任何行

SELECT     dbo.JD_tblJob.EmployerID, dbo.JD_tblJob.PostDate, dbo.JD_tblJob.JobID, dbo.JD_tblJob.JobTitle, dbo.JD_tblJob.JobDescr, dbo.JD_tblJob.MinWorkExp, dbo.JD_tblJob.MaxWorkExp, dbo.JD_tblJob.MinSalary, dbo.JD_tblJob.MaxSalary, dbo.JD_tblJob.HideSalary, dbo.JD_tblJob.NoOfVacancies, dbo.JD_tblJob.JobLocation, dbo.JD_tblJob.Active, dbo.JD_tblEmployer.CompanyName, tblKeySkill.fldName AS KeySkillName, tblFuncArea.fldName AS FuncAreaName FROM dbo.JD_tblJob INNER JOIN dbo.JD_tblMaster AS tblFuncArea ON dbo.JD_tblJob.FuncAreaID = tblFuncArea.ID INNER JOIN dbo.JD_tblMaster AS tblKeySkill ON dbo.JD_tblJob.KeySkillID = tblKeySkill.ID INNER JOIN dbo.JD_tblEmployer ON dbo.JD_tblJob.EmployerID = dbo.JD_tblEmployer.ID WHERE dbo.JD_tblJob.JobTitle + tblKeySkill.fldName + tblFuncArea.fldName LIKE '%Engineer%Software%')

答案 2 :(得分:0)

--What you are searching for
DECLARE @yourSearch varchar(50) = 'Software Developer'

DECLARE @strs varchar(200)
SET @strs = REPLACE(@yourSearch,' ','.')
DECLARE @searchWord1 varchar(50);
DECLARE @searchWord2 varchar(50);

--Split search into 2 words
SELECT
  @searchWord1 = PARSENAME(@strs,1),
  @searchWord2 = PARSENAME(@strs,2)

--Do the select
SELECT * FROM TableName 
WHERE 
  CONCAT(COALESCE( Col1, ''),COALESCE( Col2, ''),COALESCE( Col3, '')) LIKE '%' + @searchWord1 +  '%'
  OR 
  CONCAT(COALESCE( Col1, ''),COALESCE( Col2, ''),COALESCE( Col3, '')) LIKE '%' + @searchWord2 +  '%'