mysql 日期滞后,如何在mysql中执行滞后操作

这篇博客探讨了如何在MySQL中实现类似Oracle LAG函数的功能。通过使用用户变量,可以在MySQL中模拟分析函数LAG的操作。作者提供了一个示例,展示了如何在没有内置LAG函数的情况下,根据UID和Operation获取前一个操作。虽然MySQL不直接支持LAG,但通过用户变量和适当的查询结构,可以达到相同的效果。
摘要由CSDN通过智能技术生成

Guys I want to use analytical function lag in mysql. In Oracle it is supported but I can't do it in Mysql. So can anybody help me how to perform lag operation in Mysql?

For example

UID Operation

1 Logged-in

2 View content

3 Review

I want to use lag function so that my output would be as follows

UID Operation Lagoperation

1 Logged-in

2 View content Logged-in

3 Review View content

Does Mysql support lag function???

解决方案

You can emulate it with user variables:

select uid, operation, previous_operation from (

select

y.*

, @prev AS previous_Operation

, @prev := Operation

from

your_table y

, (select @prev:=NULL) vars

order by uid

) subquery_alias

see it working in an sqlfiddle live

Here you initialize your variable(s). It's the same as writing SET @prev:=NULL; before writing your query.

, (select @prev:=NULL) vars

Then the order of these statements in the select clause is important:

, @prev AS previous_Operation

, @prev := Operation

The first just displays the variables value, the second assigns the value of the current row to the variable.

It's also important to have an ORDER BY clause, as the output is otherwise not deterministic.

All this is put into a subquery just out of aesthetic reasons,... to filter out this

, @prev := Operation

column.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值