基于优先级队列搜索

时间:2015-08-20 07:58:21

标签: sql-server asp.net-mvc-4

我需要一些帮助..我正在尝试实施搜索结果

条件

1#它应该获取的记录等于用户在文本框中输入的关键字(@SEARCH)

2#连同第一个条件它还会获取在文本框中输入的匹配记录(@SEARCH)

3#搜索到的哪个关键字应该显示第一个匹配和其他匹配    之后显示(我的意思是优先队列)

4#应该检查@loc paramater并获取该关键字。

@SEARCH NVARCHAR(100) = NULL,
@loc NVARCHAR(100) = NULL
as
select *
from tblBusinessCategory as b
    inner join tblUser as u on b.BusinessID=u.BusinessCategoryId
    inner join tblAddress as a on u.AddressId=a.AddressID
where b.BusinessName = @SEARCH and
a.City = @loc 
       OR a.State = @loc 
       and  b.BusinessName LIKE '%' + @SEARCH + '%'

1 个答案:

答案 0 :(得分:0)

您必须根据该条件订购它们(b.BusinessName = @SEARCH):

select *
from tblBusinessCategory as b
    inner join tblUser as u on b.BusinessID=u.BusinessCategoryId
    inner join tblAddress as a on u.AddressId=a.AddressID
where a.City = @loc OR a.State = @loc 
       OR b.BusinessName LIKE '%' + @SEARCH + '%'
Order By case when b.BusinessName = @SEARCH then 0 else 1 end

我不确定你的where子句中是否需要OR或AND。如果要混合OR和AND,则应使用括号:where (X = 0 or Y = 5) And Z = 20