Home Database Mysql Tutorial MySql,Sqlserver,Oracle数据的分页语句

MySql,Sqlserver,Oracle数据的分页语句

Jun 07, 2016 pm 03:52 PM
mysql oracle sqlserver Pagination data

在实际项目中分页是常见的不能再说了,这里我总结了MySql,SqlServer,Oracle这三个数据库的sql分页语句 在这三个数据库中,个人觉得MySql的分页语句是最简单的,只用一个limit关键字就能完成 MySql数据库: select * from test limit 5,5; test:表名 第一个

在实际项目中分页是常见的不能再说了,这里我总结了MySql,SqlServer,Oracle这三个数据库的sql分页语句

在这三个数据库中,个人觉得MySql的分页语句是最简单的,只用一个limit关键字就能完成

MySql数据库:

select  * from test limit 5,5;

test:表名

第一个参数5:从表中的第几行开始查,从0开始数

第二个参数5:查询出多少条记录

在项目中运用时,如果一个页面指定了显示多少条记录,通常第二个参数不会改变,都是该变的第一个参数

计算每页的公式:这里都是每页显示5条记录

第一页:(1-1)*5

第二页:(2-1)*5

第三页:(3-1)*5

第N页:(n-1)*5

公式:

开始的行数=(当前页-1)*每页显示的行数

所以最后在代码中的sql语句可以写为:

int pageSize=5;//每页显示行数

int pageNo=4;//当前页

int num=(pageNo-1)*pageSize;//开始查找的行

select * from test limit num,pageSize;

我了解的MySql数据的分页语句就只有这一种了


在SqlServer数据库中目前我了解的sql分页语句有两种

第一种:

select top(5) * from test 
	where id not in(
		select top((2-1)*5) id from test
	)
Copy after login

第二种:

select * from(
	select ROW_NUMBER() Over(order by id) as rownum,* from test
	) t 
where rownum between (2-1)*5+1 and 2*5
Copy after login


在Oracle数据库中我了解的一只有两种:

第一种:

select * from (
       select t.*,rownum rn from emp t 
       ) t1 
where rn>(2-1)*3 and rn
<p>也可以使用between</p>
<pre class="brush:php;toolbar:false">select * from (
       select t.*,rownum rn from emp t
       ) 
where rn between (2-1)*3+1 and 2*3
Copy after login

第二种:

select * from (
       select t.*,rownum rn from (
              select * from emp
       )t where rownum(2-1)*3
Copy after login

这两种比较起来,个人认为第一种查询相对比较快,因为第一种只查询了两次,而第二种查询的三次

至于怎样使用,看个人比较适合哪一种了






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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How long will Oracle database logs be kept? How long will Oracle database logs be kept? May 10, 2024 am 03:27 AM

The retention period of Oracle database logs depends on the log type and configuration, including: Redo logs: determined by the maximum size configured with the "LOG_ARCHIVE_DEST" parameter. Archived redo logs: Determined by the maximum size configured by the "DB_RECOVERY_FILE_DEST_SIZE" parameter. Online redo logs: not archived, lost when the database is restarted, and the retention period is consistent with the instance running time. Audit log: Configured by the "AUDIT_TRAIL" parameter, retained for 30 days by default.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

Oracle database server hardware configuration requirements Oracle database server hardware configuration requirements May 10, 2024 am 04:00 AM

Oracle database server hardware configuration requirements: Processor: multi-core, with a main frequency of at least 2.5 GHz. For large databases, 32 cores or more are recommended. Memory: At least 8GB for small databases, 16-64GB for medium sizes, up to 512GB or more for large databases or heavy workloads. Storage: SSD or NVMe disks, RAID arrays for redundancy and performance. Network: High-speed network (10GbE or higher), dedicated network card, low-latency network. Others: Stable power supply, redundant components, compatible operating system and software, heat dissipation and cooling system.

How much memory does oracle require? How much memory does oracle require? May 10, 2024 am 04:12 AM

The amount of memory required by Oracle depends on database size, activity level, and required performance level: for storing data buffers, index buffers, executing SQL statements, and managing the data dictionary cache. The exact amount is affected by database size, activity level, and required performance level. Best practices include setting the appropriate SGA size, sizing SGA components, using AMM, and monitoring memory usage.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

See all articles