使用 LINQ 我正在查询数据进行排序,我得到了正确的答案。
这是我的数据和 linq 查询......请看看。
void Main()
{
List<SearchResult> list = new List<SearchResult>() {
new SearchResult(){ID=1,Title="Geo Prism GEO 1995 GEO* - ABS #16213899"},
new SearchResult(){ID=2,Title="Excavator JCB - ECU P/N: 728/35700"},
new SearchResult(){ID=3,Title="Geo Prism GEO 1995 - ABS #16213899"},
new SearchResult(){ID=4,Title="JCB Excavator JCB- ECU P/N: 728/35700"},
new SearchResult(){ID=5,Title="Geo Prism GEO,GEO 1995 - ABS #16213899 GEO"},
new SearchResult(){ID=6,Title="dog"},
};
var to_search = new[] { "Geo", "JCB" }.Select(x => x.ToLower()).ToArray();
var result = from searchResult in list
let title = searchResult.Title.ToLower()
let key_string = to_search.FirstOrDefault(ts => title.Contains(ts))
orderby key_string == null ? -1 : title.Split(new[] { key_string }, StringSplitOptions.None).Length descending
group searchResult by key_string into Group
orderby Group.Count() descending
select Group;
result.Dump();
}
public class SearchResult
{
public int ID { get; set; }
public string Title { get; set; }
}
我的问题是我需要写什么 sql 查询,结果我将在 sql server 2000 中得到相同的输出。假设我的数据以这种方式存储在 sql server 表中
Table : MyTable
------------------
ID Title
-----------
1 Geo Prism GEO 1995 GEO* - ABS #16213899
2 Excavator JCB - ECU P/N: 728/35700
3 Geo Prism GEO 1995 - ABS #16213899
4 JCB Excavator JCB- ECU P/N: 728/35700
5 Geo Prism GEO,GEO 1995 - ABS #16213899 GEO
6 Maruti gear box #ABS 4587
我的编辑
我在你的 sql 中修复了一些语法问题....看看
CREATE FUNCTION [dbo].[Split] (@String varchar(8000), @Delimiter char(1))
returns @temptable TABLE (items varchar(8000))
as
begin
declare @idx int
declare @slice varchar(8000)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
DECLARE @Sterm varchar(MAX)
SET @Sterm ='GEO JCB'
;WITH SearchResult (rnum, title)
as
(
(select 1 as rnum,'Geo Prism GEO 1995 GEO* - ABS #16213899' as title)
union all
(select 2 as rnum,'Excavator JCB - ECU P/N: 728/35700' as title)
union all
(select 3 as rnum,'Geo Prism GEO 1995 - ABS #16213899' as title)
union all
(select 4 as rnum,'JCB Excavator JCB- ECU P/N: 728/35700' as title)
union all
(select 5 as rnum,'Geo Prism GEO,GEO 1995 - ABS #16213899 GEO' as title)
union all
(select 6 as rnum,'dog' as title)
)
select rnum, title from SearchResult
join
( select lower(Items) as term
from dbo.Split(@Sterm , ' ')
) as search_terms
on lower(SearchResult.title) like '%' + search_terms.term +'%'
order by
search_terms.term,
(select count(*)
from dbo.Split(lower(SearchResult.title),' ')
where Items = search_terms.term
) desc
但这并不能完全满足我的要求。
1)查询应该返回搜索词以及那些与搜索词无关的行。
2)第二类应该在那里,就像那些行将首先出现在搜索词找到最大时间的地方,如 GEO 和 JCB。
您会根据搜索词的出现按降序错过第二个订单。如果你看到图像非常正确,那么你可以看到我在这里问的是什么类型的输出。谢谢。