日期:2014-05-19  浏览次数:20504 次

查询问题,路过看下。
表A
区域               地址                  
-------------------------  
普陀区             镇坪路176弄          
普陀区             镇坪路177弄    
普陀区             镇坪路180弄      
普陀区             镇坪路181弄
普陀区             曹杨路22弄

我想根据我输入的   地址取最接近的三个。

  如:输入   镇坪路176弄
出来::
区域               地址                  
-------------------------  
普陀区             镇坪路176弄          
普陀区             镇坪路177弄    
普陀区             镇坪路180弄  


------解决方案--------------------
太难了,不会

------解决方案--------------------
这么智能化的东西SQL做不了吧.
------解决方案--------------------
地址 里面怎么样知道是最近的呢?
------解决方案--------------------
是啊,要是输入的是:曹杨路22弄
这里面就一个曹杨路怎么判断最近啊
------解决方案--------------------
declare @test table (区域 varchar(50), 地址 varchar(50))
insert @test
select '普陀区 ', '镇坪路176弄 '
union all select '普陀区 ', '镇坪路177弄 '
union all select '普陀区 ', '镇坪路180弄 '
union all select '普陀区 ', '镇坪路181弄 '
union all select '普陀区 ', '曹杨路22弄 '
select 区域, 地址 from @test a where 地址 in (select top 3 地址 from @test where 地址 like '曹杨路__弄 ' and 地址 > = '曹杨路22弄 ' and 区域 = a.区域) order by 区域, 地址
select 区域, 地址 from @test a where 地址 in (select top 3 地址 from @test where 地址 like '镇坪路___弄 ' and 地址 > = '镇坪路176弄 ' and 区域 = a.区域) order by 区域, 地址
select 区域, 地址 from @test a where 地址 in (select top 3 地址 from @test where 地址 like '镇坪路___弄 ' and 地址 > = '镇坪路177弄 ' and 区域 = a.区域) order by 区域, 地址
select 区域, 地址 from @test a where 地址 in (select top 3 地址 from @test where 地址 like '镇坪路___弄 ' and 地址 > = '镇坪路181弄 ' and 区域 = a.区域) order by 区域, 地址

------解决方案--------------------
弄出来,学一招!