Update 2:
I guess it's easier to accomplish what i wanted using PHP than using SQL query, so the question can now be safely dismissed...
Thanks for all the help. Really appreciate the lesson -)
Update 1:
To make things more complicated, i added a column in the table called 'status'.
Now, is it possible to find out the adjacent rows with the same status, given the name to search for?
Example, I am given name = 'six', and i'm supposed to find two adjacent rows with the same status='y', which happends to have id=4 and id=6.
Oh yah, just a background information on what i'm trying to do (and no, it's not homework.. I wish i had homework ...
)
I'm trying to design a newsletter page with PHP/MySQL, and the user can post new messages to the database. When each message is viewed, it will have a navigation link to the previous and next message, which is the adjacent records in the database.
(The database below is just a significantly simplified version of what it will look like)
If a certain message is disabled (with status = 'n'), then the links to previous and next message should also be dynamically changed.
Now that i think about it ...
Is it easier to just read in all the messages into a PHP array, sort it based on ID and status, then do another SQL query to search for the current record + the two adjacent to it?
Or it's easier to just do it all from SQL?
Thanks.
Original post:
Assume that i have a table as attached in my mySQL table:
How do I select the row with name = 'six', as well as the two rows adjacent to it? Basically searching with name = 'six' will give me Id=6, and i want records with id = 5, 6, and 7
Is it possible without introducing subquery (SELECT within SELECT)?
Thanks.
I guess it's easier to accomplish what i wanted using PHP than using SQL query, so the question can now be safely dismissed...
Thanks for all the help. Really appreciate the lesson -)
Update 1:
To make things more complicated, i added a column in the table called 'status'.
Now, is it possible to find out the adjacent rows with the same status, given the name to search for?
Example, I am given name = 'six', and i'm supposed to find two adjacent rows with the same status='y', which happends to have id=4 and id=6.
Oh yah, just a background information on what i'm trying to do (and no, it's not homework.. I wish i had homework ...
I'm trying to design a newsletter page with PHP/MySQL, and the user can post new messages to the database. When each message is viewed, it will have a navigation link to the previous and next message, which is the adjacent records in the database.
(The database below is just a significantly simplified version of what it will look like)
If a certain message is disabled (with status = 'n'), then the links to previous and next message should also be dynamically changed.
Now that i think about it ...
Is it easier to just read in all the messages into a PHP array, sort it based on ID and status, then do another SQL query to search for the current record + the two adjacent to it?
Or it's easier to just do it all from SQL?
Thanks.
Original post:
Assume that i have a table as attached in my mySQL table:
How do I select the row with name = 'six', as well as the two rows adjacent to it? Basically searching with name = 'six' will give me Id=6, and i want records with id = 5, 6, and 7
Is it possible without introducing subquery (SELECT within SELECT)?
Thanks.