2015-11-13 40 views
1

我需要在多個列中搜索一個字符串與或條件&也希望通過空格拆分字符串。防爆。如果用戶搜索'軟件開發人員'那麼一個簡單的查詢將會像步驟來搜索字符串與拆分

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' 

東西,我可以工作在上面的查詢,但我需要知道,有沒有,我可以做任何簡單,快速和更好的查詢。

+1

根據你的問題,你真正需要的是[FTS](https://msdn.microsoft.com/en-us/library/ms142571.aspx) – ughai

+0

我想我不能在GoDaddy Shared Hosting Database中使用FTS 。 ! – Yusuf

回答

0

你瘦,你可以使用查詢像

選擇從表*,其中(COL1 + COL2 + COL3 LIKE '%軟件%')或(COL1 + COL2 + COL3 LIKE '%工程師%' )

+0

它不工作.. – Yusuf

+0

我正在使用這個相同的查詢應用程序相關的retreival之一,它的工作很好。你能告訴我你生成的SQL嗎? –

+0

如果列中只有'Software'沒有'Developer',您的解決方案將不起作用 – ughai

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%') 
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 + '%'