Home > Database > Mysql Tutorial > How to Select Every Nth Row from a MySQL Database?

How to Select Every Nth Row from a MySQL Database?

Linda Hamilton
Release: 2024-12-13 03:43:10
Original
482 people have browsed it

How to Select Every Nth Row from a MySQL Database?

Selecting Every N-th Row from MySQL Databases

To extract specific intervals of data from a MySQL database, consider resampling the data by selecting every nth row. This technique is useful when reducing the resolution of a large dataset while retaining essential trends.

Solution 1: Using a Rownum Counter and Modulo Calculation

This approach utilizes a rownum column to assign row numbers to each data point and then performs a modulo operation to filter the rows. The following query selects every 5th row:

SELECT * 
FROM ( 
    SELECT 
        @row := @row +1 AS rownum, [column name] 
    FROM ( 
        SELECT @row :=0) r, [table name] 
    ) ranked 
WHERE rownum % 5 = 1 
Copy after login

The above is the detailed content of How to Select Every Nth Row from a MySQL Database?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template