SQL实现分页查询的常用方法主要有以下几种:
SELECT * FROM table_name LIMIT offset, row_count;
其中,offset表示从第几条记录开始取,row_count表示取几条记录。例如,从第6条记录开始取,取5条记录,则为:
SELECT * FROM table_name LIMIT 5, 5;
SELECT TOP row_count * FROM table_name;
其中,row_count表示取几条记录,例如取前5条记录,则为:
SELECT TOP 5 * FROM table_name;
SELECT * FROM (SELECT *, ROWNUM AS rn FROM table_name) WHERE rn > offset and rn <= offset + row_count;
其中,offset表示从第几条记录开始取,row_count表示取几条记录。例如,从第6条记录开始取,取5条记录,则为:
SELECT * FROM (SELECT *, ROWNUM AS rn FROM table_name) WHERE rn > 5 and rn <= 10;
SELECT * FROM table_name WHERE id IN (SELECT id FROM (SELECT id, ROWNUM AS rn FROM table_name ORDER BY id) WHERE rn > offset and rn <= offset + row_count);
其中,offset表示从第几条记录开始取,row_count表示取几条记录。例如,从第6条记录开始取,取5条记录,则为:
SELECT * FROM table_name WHERE id IN (SELECT id FROM (SELECT id, ROWNUM AS rn FROM table_name ORDER BY id) WHERE rn > 5 and rn <= 10);
以上就是SQL实现分页查询的常用方法,希望对大家有所帮助。
本文链接:http://task.lmcjl.com/news/2671.html