22,302
社区成员




SELECT * FROM #t1 a
WHERE NOT EXISTS(SELECT 0 FROM #tosearch b WHERE CHARINDEX(b.Search,a.Name)=0)
SELECT *
FROM 表
WHERE CONTAINS(字段名, '单词1 AND 单词2')
create table #tosearch (Search nvarchar(100))
insert into #tosearch
select 'au'
--union all select 'be'
union all select 'de'
create table #t (Name nvarchar(100))
insert into #t
select 'Haus' union all
select 'Haus to be' union all
select 'Haus destory' union all
select 'Haus to be destory'
------------------- 修改-------------------
SELECT DISTINCT a.Name FROM (
SELECT t.*,SUM( CASE WHEN CHARINDEX(t1.Search,t.Name)>0 THEN 1 ELSE 0 END )OVER(PARTITION BY t.Name) as MatchedCount,COUNT(0)OVER(PARTITION BY t.Name) AS SerchCount
FROM #t AS t,#tosearch AS t1
) a WHERE a.MatchedCount=a.SerchCount
/*结果
Name
Haus destory
Haus to be destory
*/
drop table #tosearch
drop table #t
create table #tosearch (Search nvarchar(100))
insert into #tosearch
select 'be'
union all select 'de'
create table #t1 (Name nvarchar(100))
insert into #t1
select 'Haus' union all
select 'Haus to be' union all
select 'Haus destory' union all
select 'Haus to be destory'
SELECT * FROM #t1 a
WHERE NOT EXISTS(SELECT top 1 'x' FROM #tosearch b WHERE a.Name LIKE '%'+b.Search+'%')
drop table #tosearch
drop table #t1
这个是sql server的写法
SELECT * FROM #t AS t
CROSS APPLY(
SELECT * FROM (
SELECT 'notmatched' AS flg, t.Name,CASE WHEN CHARINDEX(ts.Search,t.Name)=0 THEN 1 ELSE 0 END AS notfound FROM #tosearch AS ts
) p1 PIVOT(SUM(notfound) for flg IN ([notmatched]))p2
) s WHERE s.[notmatched]=0