0

I have a large database with temperature values from April last year and I'm displaying all of them using JPGraph (along with different graphs for the last 2 days, last week etc.). Since there are so many values the graph doesn't look very nice and I want to display 50 values distributed evenly over the entire period.

I know I can get the total number of rows in the databse (x), divide that number by 50 (x/50 = n) and then get every n'th row.

Is there a MySQL query that can do this more efficiently?

Thanks!

knorrhane
  • 111
  • 1
  • 1
  • 5
  • 2
    Perhaps use a [user variable to generate a row index](http://stackoverflow.com/questions/13550750/how-to-add-row-index-as-a-column-to-sql-select-query), and then select every n-th row by doing a modulo check on that row index in the WHERE clause … – CBroe Feb 14 '16 at 01:54

1 Answers1

0

Here's an implementation of @CBroe's suggestion:

select *
from (
  select @row:=@row+1 row_num, a.*
  from mytable a
  join (select @row:=0) b) a
where row_num % 50 = 0;
Fabricator
  • 12,722
  • 2
  • 27
  • 40