前提:comm延续
sal_1:差分
sal_2:延3差分
select u1.empno,u1.sal,u1.comm,u1.sal_1,u2.sal_2
from
(select t.empno,t.sal,t.comm,sal-(select sal from test.emp t2 where t2.comm +1 = t.comm order by comm) as sal_1
from test.emp t
order by comm) u1 LEFT JOIN
(select t.comm+3 as comm,sal-(select sal from test.emp t2 where t2.comm +1 = t.comm order by comm) as sal_2
from test.emp t
order by comm) u2
ON u1.comm = u2.comm;
在MySQL4.1中子查询是不能使用LIMIT的,手册中也明确指明 This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME subquery’
也就是说,这样的语句是不能正确执行的。
select * from table where id in (select id from table limit 10);
但是,只要你再来一层就行。。如:
select * from table where id in (select t.id from (select * from table limit 10)as t)
select u1.empno,u1.sal,u1.comm,u1.sal_1,u2.sal_2
from
(select t.empno,t.sal,t.comm,sal-(select sal from test.emp t2 where t2.comm +1 = t.comm order by comm) as sal_1
from test.emp t
order by comm) u1 LEFT JOIN
(select t.comm+3 as comm,sal-(select sal from test.emp t2 where t2.comm +1 = t.comm order by comm) as sal_2
from test.emp t
order by comm) u2
ON u1.comm = u2.comm where
u1.empno not in (
select * from
(
select u.empno
from (
select u1.empno,u1.sal,u1.comm,u1.sal_1,u2.sal_2
from
(select t.empno,t.sal,t.comm,sal-(select sal from test.emp t2 where t2.comm +1 = t.comm order by comm) as sal_1
from test.emp t
order by comm) u1 LEFT JOIN
(select t.comm+3 as comm,sal-(select sal from test.emp t2 where t2.comm +1 = t.comm order by comm) as sal_2
from test.emp t
order by comm) u2
ON u1.comm = u2.comm) u limit 4) as t
)
empno, sal, comm, sal_1, sal_2
999, 808, 80, , 998, 885, 81, 77, 997, 807, 82, -78, 996, 804, 83, -3, 995, 855, 84, 51, 77 994, 888, 85, 33, -78 993, 856, 86, -32, -3