how can i convert this script of sqlserver to mysql with the same logic? : [enter image description here][1]


  [1]: https://i.stack.imgur.com/VfLAV.png


i have this script in sqlserver but how can i use this logic to mySQL.. it has different syntax. thanks in advance for the help.