日期:2014-05-18  浏览次数:20538 次

在一个字段里比较大小???急
在一个字段里有
  序号 价格  
  1 100,150,400,450,668  
 
  2 350,510,600,750,768  

  3 250,450,460,550,668  

我想查出价格在 200-400之间的,或300-500之间的,也可能是100-500

 求高手????

------解决方案--------------------
先拆分,再查询.
------解决方案--------------------
SQL code
create table tb(序号 int,价格 varchar(20))
insert into tb select 1,'100,150,400,450,668'
insert into tb select 2,'350,510,600,750,768'
insert into tb select 3,'250,450,460,550,668'
go
declare @min int,@max int
set @min=200
set @max=400    --你可以改变这两个值
select distinct 序号 from(
select a.序号,substring(a.价格,b.number,charindex(',',a.价格+',',b.number+1)-b.number)价格
from tb a,master..spt_values b
where b.type='p' and b.number<=len(a.价格) and substring(a.价格,b.number,1)<>',' and substring(','+a.价格,b.number,1)=','
)t where 价格 between @min and @max
/*
序号
-----------
1
2
3

(3 行受影响)

*/
go
drop table tb

------解决方案--------------------
合并拆分列 N多
------解决方案--------------------
lz是否可以考虑在程序端实现呢。
非要在数据库里实现的话。效率不一定会高。。
------解决方案--------------------
拆了再查!