user1505027
user1505027

Reputation: 343

Mysql, select rows according to date

The thing I would like to do:

Select the row where date = 2012-10-14 and then display 4 rows which go after that row. So from this list

2012-10-12 column #2
2012-10-13 column #2
2012-10-14 was very sunny.
2012-10-15 rained all day.
2012-10-16 whatever.
2012-10-17 column #2
2012-10-18 column #2
2012-10-19 rained all day.
2012-10-20 whatever.
2012-10-21 column #2
2012-10-22 column #2

it would return this:

2012-10-14 was very sunny.
2012-10-15 rained all day.
2012-10-16 whatever.
2012-10-17 column #2
2012-10-18 column #2

Thanks for help.

PS: In the database there are no data on weekends so some of the dates will be missing.

Upvotes: 1

Views: 156

Answers (2)

Alex Monthy
Alex Monthy

Reputation: 1877

This version is for a date range rather than for a fixed number of rows. It is not completely clear which one you want.

select * from mytable where date_diff(date,'2012-10-14') <= 4 and date >= '2012-10-14';

Upvotes: 1

eggyal
eggyal

Reputation: 125835

SELECT * FROM my_table WHERE date >= '2012-10-14' ORDER BY date LIMIT 5

See it on sqlfiddle.

Upvotes: 5

Related Questions