I have table:
id | topic | subjectId | content |
---|---|---|---|
1 | Topic1 | 5 | con… |
2 | topic2 | 6 | con… |
3 | topic3 | 6 | con…. |
4 | Topic4 | 5 | con… |
5 | topic5 | 6 | con… |
6 | topic6 | 5 | con…. |
currently I selected id 4 which have subjectId 5, How to select previous id with subjectId 5, and next id with subjectId
3
Answers
I’m not 100% sure I understand your question, but from what I think your trying to do is get
id 1
to return based on passid 4
to a query.This query does that, if you make it an stored procedure and pass an id, it will return the data you need to do whatever that is:
You could retrieve the next and previous ids at the same time as fetching the current topic:
Which will return:
Here’s a db<>fiddle
Since MySQL 8.0 you can use window function
lead
&lag
in next way:https://sqlize.online/s/gi