我有一个包含 20 个(或更多)单词(字符串)的列表,并且想要选择在其列的 3 个中包含这些单词的行。我应该使用like
sql 的表达式。但我不知道如何在like 表达式中使用多个字符串。(我union
现在这样做,但我至少有 60 个 select 语句并认为它降低了性能,真的降低性能吗?)
//get the advertise that have similar keywords
foreach (string str in keywords)
{
if (str != "")
{
if (!string.IsNullOrEmpty(sqlQuery)) sqlQuery += " union";
sqlQuery = "select * from AD_Advertise where (AdKeyWords like N'%" + str + "%'"
+ " OR AdTitle like N'%" + str + "%' "
+ " OR AdDescription like N'%" + str + "%' "
+ " OR AdGroupItemCode=" + adinfo.AdGroupItemCode + ")"
+ " AND AdSiteID=" + CMSContext.CurrentSiteID
+ " AND AdShow='True' "
+ " AND ItemID != " + ADId;
}
}
ds = cn.ExecuteQuery(sqlQuery,null);//("AD.Advertise.selectall", null, where, "ItemModifiedWhen");
回答:
最后我使用了下面的代码:
if object_id('tempdb..#WordList') is not null
drop table #WordList
CREATE TABLE #WordList ( KeyWord nvarchar(100))
insert into #WordList values (N'حقوقی'),(N'وکیل');
SELECT DISTINCT *
FROM AD_ADvertise a
LEFT JOIN #WordList k
ON a.AdKeywords LIKE '%' + k.KeyWord + '%'
OR a.AdTitle LIKE '%' + k.KeyWord + '%'
OR a.AdDescription LIKE '%' + k.KeyWord + '%'
WHERE
(k.KeyWord IS NOT NULL OR a.AdGroupItemCode = @AdGroupItemCode)
AND a.AdSiteId = @AdSiteId
AND a.AdShow = 'True'
AND a.ItemId != @ItemId
;drop table #WordList