查询以搜索多个列

时间:2014-06-29 06:36:58

标签: sql sql-server full-text-search

我有类似的表格如下:

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秒作为州,第三个作为国家/地区包含,则它应该首先出现。

请帮助我。

1 个答案:

答案 0 :(得分: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