search
Homephp教程php手册讨论SELECT TOP N 问题

讨论SELECT TOP N 问题

Jun 13, 2016 am 10:04 AM
everyoneselecttopNowcomputerdiscussquestion

Hi, everyone:
    本贴讨论SELECT TOP N问题.
    现在正在一家计算机公司兼职,开发ASP应用. 在最近的一个商业项目里有这样一个需求:用户在查询时,只需要按照某(些)列排序后TOP 20的纪录. SQL SERVER 7很好地支持了TOP N 操作,但考虑到系统移植问题, 我又考虑在其他几个主流DBMSs中如何实现. 鉴于只有DB2 UDB 7,ORACLE 8i,SQL SERVER 7,本贴仅讨论这三个DBMS.
    简单地说,TOP N问题就是:在SELECT中,仅选择按照某(些)列排序后TOP N的纪录. 考虑到等值问题,又可以分为两种: 一是仅仅返回N条纪录(M 1), 二是还包括所有于第N条等值的纪录(M 2). 当然最内层的子查询也可以有其他的子句, 或者TOP N也可以应用在没有ORDER BY的情况下,这样更简单.
1. SQL SERVER 7: 用 TOP N (WITH TIES)
M1:
SELECT TOP N * FROM MYTABLE ORDER BY ORD_COL;
M2:
SELECT TOP N    WITH TIES * FROM MYTABLE ORDER BY ORD_COL;
注: SQL SERVER 7提供了PERCENT N WITH TIES, ACCESS 中提供了TOP N,但含义是M 2.
2. ORACLE 8i: 用 ROWNUMM1:
SELECT * FROM
     ( SELECT * FROM MYTABLE ORDER BY ORD_COL DESC)
WHERE ROWNUMM2:
SELECT * FROM MYTABLE WHERE ORD_COL>=
     (SELECT MIN(ORD_COL) FROM
        ( SELECT * FROM MYTABLE ORDER BY ORD_COL DESC)
    WHERE ROWNUMORDER BY ORD_COL DESC
注意以下两种错误用法:
WRONG 1:
SELECT * FROM MYTABLE
WHERE    ROWIDORDER BY ORD_COL DESC;
WRONG 2:(因为WHERE ROWNUMSELECT * FROM MYTABLE
WHERE    ROWNUMORDER BY ORD_COL DESC;
3: DB2
用FETCH FIRST N ROWS ONLY
M1:
SELECT * FROM MYTABLE
ORDER BY ORD_COL DESC
FETCH FIRST N ROWS ONLY
M2:
没有找到,因为DB2不允许在FROM中嵌套有ORDER BY子句的子查询.
还不清楚ORACLE的M 2有没有更好的办法,以及其他的DBMS如何实现TOP N操作,请其他朋友补充.

Statement
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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.