2014-06-29 189 views
-1

我有類似的表如下:查詢搜索多個列

Name city state country 
---- ---- ----- ------- 
Sree mm  ap  ind 
SRee redmond ny  us 
rahul hyd  ap  ind 
xxx  mm  ap  ind 
abcd mm  tn  ind 
wer  dd  ap  ind 

如果我mm,ap尋找我需要得到這些。

Name city state country 
---- ---- ----- ------- 
Sree mm  ap  ind 
xxx  mm  ap  ind 
abcd mm  tn  ind 
wer  dd  ap  ind 

如果這兩個詞相匹配,應該是第一位的,如果包含city秒州和第三作爲國家。

請幫我這個。

回答

2
declare @city nvarchar(50) = 'mm', 
     @state nvarchar(50) = 'ap' 

Select * 
From (Select * 
    from YourTable 
    Where city = @city or state = @state 
    )z 
order by Case When city = @city and state = @state Then 1 
       When City = @city then 2 
       Else 3 
      end