搜索最小列

Search for Minimum Columns

支持用户在 ASP.NET

的屏幕中选择 10 列进行搜索

我们需要在数据库中搜索至少 8 个匹配的列

如何编写 sql 查询以匹配 10 列中的任意 8 列

我们只需要匹配 10 列中的 8 列

您需要一个 表达式 来显示匹配的列数,然后将其与需要匹配的列数进行比较。

像这样:

where
-- How many columns matched? 
-- Score 1 for each matching column
0
+ case when column1 = @search1 then 1 else 0 end
+ case when column2 = @search2 then 1 else 0 end
+ case when column3 = @search3 then 1 else 0 end
+ case when column4 = @search4 then 1 else 0 end
+ case when column5 = @search5 then 1 else 0 end
+ case when column6 = @search6 then 1 else 0 end
+ case when column7 = @search7 then 1 else 0 end
+ case when column8 = @search8 then 1 else 0 end
+ case when column9 = @search9 then 1 else 0 end
+ case when column10 = @search10 then 1 else 0 end

>= 8

您可以推广这种方法,为不同的列给出不同的分数。

请注意,此方法不会在任何搜索列上使用索引。

但是,如果一些列有索引,您可能可以通过使用它们来提高性能。 (要匹配十列中的八列,使用索引,至少三列必须有索引)。