0

Let's assume that in database are these values:

time_column

2013-03-02 19:00:00
2013-03-02 20:00:00
2013-03-02 21:00:00
2013-03-02 22:00:00

I need to get the last 3 records include with the value from a row, which is in the query.

SELECT * FROM table WHERE time_column =< '2013-03-02 22:00:00' LIMIT 3

This query returns these rows (no 22:00:00):

2013-03-02 19:00:00
2013-03-02 20:00:00
2013-03-02 21:00:00

But I need to get these ones:

2013-03-02 20:00:00
2013-03-02 21:00:00
2013-03-02 22:00:00

But how to do that?

Thank you

EDIT: There is a way to get the needed rows on the application layer, but if would be possible to get that from a sql, that would be great.

1
  • It doesn't have anything to do with the compared value not being included - that's just an artifact of the sample data. Commented Mar 2, 2013 at 19:03

1 Answer 1

3
SELECT * FROM table WHERE time_column =< '2013-03-02 22:00:00'
ORDER BY time_column DESC
LIMIT 3
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I need a vacation! My question looks like I just came to the computer for the first time... However, thanks Marlin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.