Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you select every n-th row from mysql

Tags:

mysql

I have a series of values in a database that I need to pull to create a line chart. Because i dont require high resolution I would like to resample the data by selecting every 5th row from the database.

like image 958
Corban Brook Avatar asked May 13 '09 15:05

Corban Brook


People also ask

How do you select every row in a given table?

To select non-adjacent rows or columns, hold Ctrl and select the row or column numbers.

How do I select multiple rows in MySQL?

To select last two rows, use ORDER BY DESC LIMIT 2.

How do I select specific rows in SQL?

To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.


2 Answers

SELECT *  FROM (      SELECT          @row := @row +1 AS rownum, [column name]      FROM (          SELECT @row :=0) r, [table name]      ) ranked  WHERE rownum % [n] = 1  
like image 165
Taylor Leese Avatar answered Oct 12 '22 13:10

Taylor Leese


You could try mod 5 to get rows where the ID is multiple of 5. (Assuming you have some sort of ID column that's sequential.)

select * from table where table.id mod 5 = 0; 
like image 41
Owen Avatar answered Oct 12 '22 11:10

Owen