简体   繁体   中英

SQL loop with current and previous values

I need to loop through rows which has id and when looping i need to check current and previous values.

I have a cursor to fetch them :

DECLARE idCur CURSOR FOR SELECT MessageId FROM EMMessages;

So now i need to loop through these values and compare them when looping: For example i have ids = [1, 2, 3, 4] I want to compare 1 and 2 then 2 and 3 and so on.. How in sql i should declare this n and n-1 variable or should i decalare n and n+1.

You can refer to this previous answer

For loop example in MySQL

Else, You can use another code together with sql to achieve this. Such as using for loop in php.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM