select a.id as aid,b.id as bid,a.city,a.cang,a.sid,a.time as atime,b.time as btime,a.price as aprice,b.price as bprice,a.pm as apm,b.pm as bpm from (select id,city,cang,sid,time,price,@rank:=@rank+1 as pm from cai,(SELECT @rank:=0) B group by id,city,cang,time order by city,cang,sid,time) as a,(select id,city,cang,sid,time,price,@ranks:=@ranks+1 as pm from cai,(SELECT @ranks:=0) B group by id,city,cang,time order by city,cang,sid,time) as b where a.sid=b.sid and a.cang = b.cang and a.city = b.city and a.id<b.id and abs(a.pm-b.pm)=1 and (abs(a.price-b.price)/a.price<0.2 or abs(a.price-b.price)/b.price<0.2)
结果:
资料整理:
1.排序后插入序号
http://blog.csdn.net/k8080880/article/details/11253305
2.相邻做差参考
http://bbs.csdn.net/topics/390887686 相邻两日气温差值
http://bbs.csdn.net/topics/300153858 同行两个日期相差最小
http://zhidao.baidu.com/link?url=ic00M2Rne5CSeB9tFBizT-o_JFVRjORgn-pCOVeL5syHRQQskPJ-u5BSY70WJ9HEv3e4uBe6ohHrslYIR0NpHWvN5VH43AwgVOtj2CqxfzO 待解决一个类似的问题
3.此问题原帖
http://tieba.baidu.com/p/4200604602?pid=80233503055&cid=0#80233503055