Home Database Mysql Tutorial 数据库取前几条记录的语句

数据库取前几条记录的语句

Jun 07, 2016 pm 02:53 PM
database Record statement

数据库取前几条记录的语句 1. ORACLE www.2cto.com SELECT * FROM TABLE1 WHERE ROWNUM=N 2. INFORMIX SELECT FIRST N * FROM TABLE1 where 1=1 3. DB2 SELECT * ROW_NUMBER() OVER(ORDER BY COL1 DESC) AS ROWNUM WHERE ROWNUM=N 或者 SELECT COLUMN FROM T


数据库取前几条记录的语句

 

1. ORACLE     www.2cto.com  

SELECT * FROM TABLE1 WHERE ROWNUM

2. INFORMIX 

SELECT FIRST N * FROM TABLE1 where 1=1 

3. DB2 

SELECT * ROW_NUMBER() OVER(ORDER BY COL1 DESC) AS ROWNUM WHERE ROWNUM

或者 

SELECT COLUMN FROM TABLE where 1=1 FETCH FIRST N ROWS ONLY 

4. SQL SERVER 

SELECT TOP N * FROM TABLE1 where 1=1 

or 

SET ROWCOUNT N SELECT * FROM TABLE1 where 1=1 SET ROWCOUNT N1 

5. SYBASE 

SET ROWCOUNT N SELECT * FROM TABLE1 where 1=1 SET ROWCOUNT N1 

6. MYSQL 

SELECT * FROM TABLE1 where 1=1 LIMIT N 

7. FOXPRO 

SELECT * TOP N FROM TABLE ORDER BY COLUMN 

8. ACCESS 

SELECT TOP N * FROM TABLE1 where 1=1

 

 1. Oracle数据库

  

  SELECT * FROM TABLENAME WHERE ROWNUM

  

  2. Infomix数据库

  

  SELECT FIRST N * FROM TABLENAME

  

  3. DB2数据库

  

  SELECT *

  FROM (SELECT * ROW_NUMBER() OVER({ORDER BY COL1 DESC}) AS ROWNUM FROM TABLENAME)

  WHERE ROWNUM

  或者

  SELECT COLUMN FROM TABLENAME FETCH FIRST N ROWS ONLY

  

  4. SQL Server数据库

  

  SELECT TOP N * FROM TABLENAME

  

  5. Sybase数据库

  

  SET ROWCOUNT N

  GO

  SELECT * FROM TABLENAME

  

  6. MySQL数据库

  

  SELECT * FROM TABLENAME LIMIT N

  

  7. FoxPro数据库

  

  SELECT * TOP N FROM TABLENAME ORDER BY COLUMN 

  

  Top关键字:

  1、首先引用一位网友出错的例子,sql="select top 30 * from data where title='"&title1&"' order by id desc"

  分析出错原因:sql语句里同时存在where和top语句的时候,并且where条件列不是合适的索引,程序执行的是全表扫描,首先是查找符合where条件的记录,而这里的top限制形同虚设。如果全表是百万级别以上的数据表,那么就这么一个简单的判断,就有可能拖垮数据库。

  2、解决办法就是去掉where条件筛选语句,如果你希望使用selcet top语句,并且还要附带where条件,那么条件中的列就得是合适的索引,如聚集索引、复合索引里的主列等,同时,where条件里也要尽量避开使用函数,or,判断NULL等会引起全部扫描的语句。这一点要记住,不然执行的是全表扫描。

  3、如何选出第N条到第2N条记录呢。这样的sql语句就可以了:

  “select top n * from TABLE_NAME where id not in (select top n id from TABLE_NAME order by id desc)"

  4、对Top基本理解下(TOP 和 SET ROWCOUNT)

  SELECT 语句中的TOP子句限制单个查询返回的行数,而SET ROWCOUNT限制所有后续查询影响的行数。在很多编程任务中这些命令提供了高效率。

  

  SET ROWCOUNT在SELECT,INSERT,UPDATE OR DELETE语句中设置可以被影响的最大行数。这些设置在命令执行时马上生效并且只影响当前的会话。为了移除这个限制执行SET ROWCOUNT 0。一些实际的任务用TOP or SET ROWCOUNT比用标准的SQL命令对编程是更有效率的。让我们在几个例子中证明:

  

  在几乎所有的数据库中最流行的一个查询是请求一个列表中的前N项。在 pubs数据库案例中,我们可以查找销售最好CD的前五项。比较用TOP,SET ROWCOUNT和使用ANSI SQL的三种方案。

  (1)Select title,ytd_salesFrom titlesa Where (select count(*)From titlesb Where b.ytd_sales>a.ytd_sales)

  这个纯ANSI SQL方案执行一个效率可能很低的关联子查询,特别的在这个例子中,在ytd_sales上没有索引支持。另外,这个纯的标准SQL命令没有过滤掉在ytd_sales的空值,也没有区别多个CD间有关联的情况。

  (2)SET ROWCOUNT 5 SELECT title, ytd_salesFROM titlesORDER BY ytd_sales DESCSET ROWCOUNT 0

  (3)SELECT TOP 5 title, ytd_salesFROM titlesORDER BY ytd_sales DESC

  第二个方案使用SET ROWCOUNT来停止SELECT查询,而第三个方案是当它找到前五行时用TOP n来停止。在这种情况下,在获得结果之前我们也要有一个ORDER BY子句强制对整个表进行排序。两个查询的查询计划实际上是一样的。然而,TOP优于SET ROWCOUNT的关键点是SET必须处理ORDER BY子句所需的工作表,而TOP 不用。

  总结:在一个大表上,我们可以为表上创建一个索引以避免排序,查询将使用该索引找到前5行并停止。

  

  ROWNUM伪列:

  1、使用SELECT语句返回的结果集,希望按特定条件查询前N条记录,可以使用伪列ROWNUM。ROWNUM是对结果集加一个伪列,先查到结果集后再加上一个列,是符合条件结果的序列号;从1开始排起,只能用 = !=这些比较符合。

  ROWNUM对于等于某值的查询条件,比如查询第一条记录,可以使用ROWNUM=1作为条件;但是如果查询第二条记录写ROWNUM=2则查询不到数据,ROWNUM的=判断只对于1有效;

  2、同理,查询大于某值的查询条件,ROWNUM>n(n>1的自然数)这种条件不成立;那如何查询第一行以后的记录呢?

  solution:用子查询解决,但是必须为rownum设置别名,"select * from (select rownum no ,id,name from table1) where no >1";

  3、ROWNUM对于小于某值的查询是可以的,select * from table1 where rownum

  4、ROWNUM和排序

  Oracle中rownum是在取数据时产生的序号,想对指定排序的数据指定rownum行数据就要做处理了:

  "select rownum,id,name from table1 order by name",查询结果按name排序,但是rownum不是按1、2、3...排列,而是按记录插入时的顺序给记录排号;

  solution:使用子查询,select rownum ,id,name from(select * from table1 order by name)

 

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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 does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Tips and practices for handling Chinese garbled characters in databases with PHP Tips and practices for handling Chinese garbled characters in databases with PHP Mar 27, 2024 pm 05:21 PM

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

See all articles