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.P M as apm,b.pm as BPM from (select Id,city,cang,sid,time,price, @rank: [e-mail protected]+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: [Email protect Ed]+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)
Results:
Data collation:
1. Insert serial number after sorting
http://blog.csdn.net/k8080880/article/details/11253305
2. Adjacent to do bad reference
Temperature difference between two days adjacent to http://bbs.csdn.net/topics/390887686
http://bbs.csdn.net/topics/300153858 two-day minimum difference between dates
Http://zhidao.baidu.com/link?url=ic00M2Rne5CSeB9tFBizT-o_ Jfvrjorgn-pcovel5syhrqqskpj-u5bsy70wj9hev3e4ube6ohhrslyir0nphwvn5vh43awgvotj2cqxfzo to solve a similar problem
3. The original issue
http://tieba.baidu.com/p/4200604602?pid=80233503055&cid=0#80233503055
SQL statement-Sort by adding sequence number and then calculating the desired item