Lag and Lead functions of mysql
1. There are no lag and lead functions in mysql. it is inconvenient to use this function because Oracle is used to it. So I wonder if it can be implemented.
2. If you use a function, you can certainly implement it. But can I implement it without using a function? Through experiments, we found that the goal can be achieved.
You can write the lag Function in this way.
Select @ lagfield, @ lagfiled: = targField, t. * from table t, (select @ lagfield: = '') r
3. Here, targField is the field you want to present in the next line. The final select should be different based on whether the field is a character or a number,
1) if it is a number, it should be (select @ lagfield: = 0) r,
2) If the character is used
4. For lead, the opposite is true. You can take four steps.
1) generate rownum
2) descending order based on rownum
3) implemented in lag Mode
4) in the forward order of rownum