The lag and lead functions of MySQL

Source: Internet
Author: User

1. There is no lag and lead function in MySQL because Oracle is used to it, and no such function is inconvenient. So I wonder if I can make it.

2. If the function is used, it can certainly be realized. But if you don't use a function, can it be implemented? After testing, it was found that the aim could be achieved.

For the LAG function, you can write this

Select @lagfield, @lagfiled: =targfield,t.* from Table T, (select @lagfield: = ") r

3, where Targfield is the field you want to render on the next line, depending on whether the field is a character or a number, the last select should be different,

1) If it is a number, it should (select @lagfield: =0) R,

2) If the character is in the example


4, for lead, just can opposite, can walk four steps

1) Generate RowNum

2) according to rownum reverse

3) According to the Lag way to achieve

4) in accordance with rownum positive sequence

The lag and lead functions of MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.