我有一张表格,里面有许多公司的描述。
TABLE dbo.[TblCompanies]
(
[CompanyID] [int] IDENTITY(1,1) NOT NULL,
[CompanyProfile] [nvarchar](max) NOT NULL
)
描述看起来像那样
“安捷伦科技有限公司提供以应用为中心的解决方案 ..“
我还有另一个包含一些字符串的表
TABLE dbo.[TblFilter]
(
[Pattern] [nvarchar](100) NOT NULL
)
例如
技术
应用
如何获得所有全部 CompanyIDs
TblFilter
中所有值的公司CompanyProfile
? CompanyProfile
中过滤字符串的显示顺序不重要。 是重要的是所有这些字符串将位于CompanyProfile
。
更喜欢没有游标的解决方案。
感谢。
答案 0 :(得分:4)
尝试这样
我希望我最终能做到这一点:
任何匹配都将计为1
。这些值的SUM
必须等于表pattern
DECLARE @companies TABLE(ID INT IDENTITY, YourText VARCHAR(100));
INSERT INTO @companies VALUES
('This contains abc and xyz')
,('This contains mno')
,('This contains abc and xyz and mno')
,('This contains nothing')
,('This contains abc');
DECLARE @pattern TABLE(ID INT IDENTITY,YourPattern VARCHAR(100));
INSERT INTO @pattern VALUES
('abc')
--,('def')
,('xyz');
SELECT c.ID,c.YourText
FROM @companies AS c
CROSS JOIN @pattern AS p
GROUP BY c.ID,c.YourText
HAVING SUM(CASE WHEN c.YourText LIKE '%' + p.YourPattern + '%' THEN 1 ELSE 0 END)=(SELECT COUNT(*) FROM @pattern)
答案 1 :(得分:3)
您可以过滤掉任何模式不匹配的行:
select *
from TblCompanies c
where not exists
(
select *
from TblFilter f
where c. CompanyProfile not like '%' + f.Pattern + '%'
)
答案 2 :(得分:0)
您应该考虑创建Full Text Index
,如下所述:
https://msdn.microsoft.com/en-us/library/ms142497.aspx和此处:https://msdn.microsoft.com/en-us/library/ms142583.aspx
简而言之:
CREATE FULLTEXT CATALOG FTCat;
GO
-- There has to be a unique key for the Full Text Search to identify the row
CREATE UNIQUE INDEX ui_CompanyID ON dbo.TblCompanies(CompanyID);
GO
CREATE FULLTEXT INDEX ON dbo.TblCompanies
(
CompanyProfile --Full-text index column name
Language 2057 --2057 is the LCID for British English
)
KEY INDEX ui_CompanyID ON FTCat --Unique index
WITH CHANGE_TRACKING AUTO --Population type;
GO
然后您可以使用FREETEXT
或CONTAINS
SELECT CompanyID
,CompanyProfile
FROM dbo.TblCompanies
WHERE CONTAINS(CompanyProfile, 'technology AND applications')
答案 3 :(得分:0)
我们可以这样使用:
select * from @companies c where exists (select 1 from @pattern p where c.YourText like concat('%',p.YourPattern,'%'))