0

我希望能够在“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'
4

1 回答 1

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 子句条件已更新

于 2013-05-07T08:23:06.753 回答