mysql 差分_差分及延3差分在MySQL数据库中实现

前提: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值