troubleshooting Question

SELECT plus/minus two entries from a certain point

Avatar of edmund01
edmund01 asked on
MySQL Server
4 Comments1 Solution1251 ViewsLast Modified:
I have to select items in such a way, that when a certain news item is selected, I also need to select two older and two newer items with it (sorted by date). That would make 5 items in resultset, basically target item plus/minus two neighbours.

Is this possible with a single select?

Only way I can think of at this moment, would be to select target item, it's timestamp and then run two more selects. One with where timestamp older than the one from target item ORDER BY date DESC LIMIT 2, and one where date is newer than target timestamp ORDER BY date ASC LIMIT 2.

Any help appreciated.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros