选择具有指定id的条目,并使用相同的外键值前后一个条目[关闭]

I have a table which looks like this one:

| id | fk_book | name |
-----------------------
| 1  | 2       | test1|
| 3  | 2       | test3|
| 6  | 3       | notes|
| 7  | 2       | test2|

No I want to get the entry with the id 3. select * from test where id=3 AND fk_book=1;

but is there also a way to get the item with id 1 and 7? and I dont know the ids of the other entries

thanks

I think you need this:

select * from test where id=3 AND fk_book = 2
union all 
select * from test where id < 3 AND fk_book = 2 order by id desc limit 1
union all 
select * from test where id > 3 AND fk_book = 2 order by id asc limit 1

Try following sql query:

select * from test where id in(1,3,7);