CharIndex找到相关的单词或类似单词

时间:2013-05-07 07:53:25

标签: sql tsql

我希望能够在'where'子句中键入一个单词,并在所有表格中搜索该单词,或者可以在相似的单词中进行搜索

DECLARE @Test VARCHAR (200)
SET @Test = 'Red'

SELECT        H.Name, O.Name AS 'Owner', S.SilksName, T.Name AS 'Trainer'
FROM          Horse H
INNER JOIN Owner O on CHARINDEX(@Test,O.Name) <> 0 or
INNER JOIN Trainer T ON CHARINDEX(@Test,T.Name) <> 0 or
INNER JOIN Runners R ON Horse.Horse_Skey = Runners.Horse_Skey 
INNER JOIN Silks S ON CHARINDEX(@Test,S.SilksName) <> 0 or 
INNER JOIN Jockey J ON CHARINDEX(@Test,J.FirstName) <> 0

WHERE @Test = 'Red'

1 个答案:

答案 0 :(得分:0)

DECLARE @Test VARCHAR (200)
SET @Test = 'Red'

SELECT        H.Name, O.Name AS 'Owner', S.SilksName, T.Name AS 'Trainer'
FROM          Horse H
inner JOIN Runners R ON Horse.Horse_Skey = r.Horse_Skey 
left JOIN Owner O on CHARINDEX(@Test,O.Name) <> 0 
left JOIN Trainer T ON CHARINDEX(@Test,T.Name) <> 0     
left JOIN Silks S ON CHARINDEX(@Test,S.SilksName) <> 0  
left JOIN Jockey J ON CHARINDEX(@Test,J.FirstName) <> 0
where   ( (O.Name is not null)  or  (T.Name is not null)   or   (S.SilksName is not null)   or   (J.FirstName is not null)  )

INNER JOIN包含必须存在的表并与其他人保持联接,当然可能需要where子句中的某些条件。 where子句条件更新