且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

返回每第 n 条记录的行

更新时间:2023-02-07 08:48:17

ROW_NUMBER 可以提供帮助.它需要一个 order-by 子句,但这是可以的,因为存在一个 order-by(并且需要保证特定的订单).

This is where ROW_NUMBER can help. It requires an order-by clause but this is okay because an order-by is present (and required to guarantee a particular order).

SELECT t.id, t.key
FROM
(
    SELECT id, key, ROW_NUMBER() OVER (ORDER BY key) AS rownum
    FROM datatable
) AS t
WHERE t.rownum % 30 = 0    -- or % 40 etc
ORDER BY t.key