如何在SQL Server中使用JOIN LIKE和AND运算符?

时间:2016-09-28 11:09:43

标签: sql-server join sql-like

首先看一下这个场景:

我的table1包含C1列,其中有2条记录,其值为Text1和Text2。

SELECT * FROM TabelX
INNER JOIN table1 ON TableX.Name LIKE '%'+table1.C1+'%'

此代码表示:

SELECT * FROM TabelX
WHERE TableX.Name LIKE '%Text1%' OR TableX.Name LIKE '%Text2%'

现在我怎么能改变JOIN LIKE这意味着AND而不是OR?类似的东西:

SELECT * FROM TabelX
WHERE TableX.Name LIKE '%Text1%' AND TableX.Name LIKE '%Text2%'

1 个答案:

答案 0 :(得分:0)

我觉得这样的事情对你有用:

BEGIN
declare @mycounter integer
select @mycounter = count (*) from table1

SELECT * FROM TabelX
where @mycounter = (select * from table1 where TableX.Name LIKE '%'+table1.C1+'%')
END