last day (15 days later) » 

8:04 PM
1
A: Find the previous and after raw in mysql

Giorgos BetsosYou can use variables to achieve what you want: SELECT stop_id, name, lat, longi, rn FROM ( SELECT stop_id, name, lat, longi, @r:=@r+1 AS rn FROM stops, (SELECT @r:=0) var ORDER BY stop_id, name ) s WHERE name != 'TEST' AND rn >= (SELECT row_number FROM ( ...

 
It was a mistake but now when I execute it the first block above Demo here I am getting just the previous one and not the one after too?
 
@TheTime What do you mean by first block?
 
I mean this block which starts with SELECT stop_id, name, lat, longi, rn and not the second one which starts with SELECT name, @row_number:=@row_number+1 AS row_number
 
@TheTime Can you set up an sql fiddle demonstrating the issue?
 
What I cant unserstand why I am getting two result records when the name is ABC but just one when the name new is?
 
8:04 PM
@TheTime new is the last record, hence there is only a previous record and no next record. That's why you get only one result in this case.
@TheTime Check this fiddle. You can play around with rn, row_number and cnt in the WHERE clause of your query to get any result you want.
 
and ABC is the first one so there is just next records but I am getting 2 next records TEST, skyand not just one TEST?
 
@TheTime Check this fiddle. It provides a generic solution for all cases.
 
I still have the problem with the first name XYZ in your fiddle solution I am getting ABC, Test as result also still the both
 
The latest fiddle that I refer to has XYZ as the first record.
 
Hi, I am here now. Yes it has XYZ as first record but I am getting now for the first name the two next records instead of just the next one and the same case for the last record I am getting for it the last two previous records instead of the last previous one.
Is it possible to get just the next one if the name is ABC and just the previous one if the name is new?
 
8:15 PM
ABC is the second record in the dummy data of the fiddle. Do you mean get the next one if name is XYZ?
 
Yes please
 
I have modified the fiddle. Check it out.
Kannst du auch auf Deutsch schreiben, wenn du moechtest! =:)
 
Thats what I wanted thank you very much :)
haha ok danke schön es hat sich jetzt erledigt ;)
 

last day (15 days later) »