我有一个名为Property
的表,数据类似于
pid city state state_abb address1 address2
x1 NewCity NHANy NH xxxx gfg
x2 Gloucester Manchestar MA newAde xxxx
x3 OtherC NewYork NY yyyy
我想要一个带有搜索关键字的查询来显示顺序,如(州或州_abb),城市,地址1,地址2
例如:
如果我使用新搜索关键字,结果输出
pid city state state_abb address1 address2
x3 OtherC NewYork NY yyyy
x1 NewCity NHANy NH xxxx gfg
x2 Gloucester Manchester MA newAde xxxx
我不想要不匹配的行。我希望只显示匹配的行。
提前致谢
答案 0 :(得分:3)
它认为问题是如何订购结果,而不是如何执行搜索。试试这个作为你的ORDER BY子句
order by
case when charindex('NEW',state) >0 then 1000 else 0 end desc,
case when charindex('NEW',city) >0 then 100 else 0 end desc,
case when charindex('NEW',address) >0 then 10 else 0 end desc
Where子句只获取匹配的行
select * from Property where
city like '%New%' or
state like '%New%' or
address1 like '%New%' or
address2 like '%New%'
答案 1 :(得分:0)
您可以尝试这样的事情:
SELECT pid, city, state, stat_abb, address1, address2
from property
where city like '%New%'
OR state like '%New%'
OR address1 like '%New%'
答案 2 :(得分:0)
这应该让你开始:
select * from Property where
city like '%New%' or
state like '%New%' or
address1 like '%New%' or
address2 like '%New%'
我假设您不想查看pid
列。
答案 3 :(得分:0)
select *
from Property
order by case
when state like 'New%' then 1
when state_abb like 'New%' then 1
when city like 'New%' then 2
when address1 like 'New%' then 3
when address2 like 'New%' then 4
end