Database Select Northern to Nth + M Record [July 10, 2004 20:03]

zhaozj2021-02-16  62

It turns out that there is no serious consideration. SQL Server

Record1Record2 ... Recordn ... Recordn m ......

Result1: Select Top N M from Tablename Order By Field1; / * Field1's order is the order of the table * /

Record1Record2 ... Recordn ... Recordn M

Result2: SELECT TOP M from Result1 ORDER by Field1 DESC;

Recordn MRecordn M-1 ... Recordn

Result3: Select * from result2 order by field2;

The last statement is: SELECT * FROM (SELECT TOP N M from Tablename Order by Field1) Order By Field1 DESC) Order by Field1;

2004-7-30 Completion (suitable in Oracle below):

There is a problem with this, if m is a statement that has been ORDER BY, the outer ORDER BY may also destroy the inner layer of Order By, so it is necessary to add a layer of constraint, and Field1 must be the first field of the inner order by. After a simple test, this is ok, the test case is behind.

Another method of acquiring N to N M strip is:

SQL> Select * from (select rownum as xfdgdf, tablename. * From tablename oritable) Where xfdgdf betWeen n and N m;

The above is more than a field XfDGDF, but how is it? ?

SQL> Select Custid, CustName from Customer;

Custid custom ---------- -------------------- 35 444 35 6666 445 676 35 AAA 35 5555

SQL> Select Custid, CustName from Customer Order by Custid, Custname

Custid customAme ---------- -------------------- 35 444 35 5555 35 6666 35 AAA 445 676

SQL> SELECT * FROM (SELECT * "Order by Custid DESC) Order by Custid Desc;

Custid customAme ---------- -------------------- 35 444 35 5555 35 6666 35 AAA 445 676

July 31:

Note: SELECT * from Tablename Where Rownum> 10 can't take out the record of Rownum behind Rownum

Today, another method is found, but there is no test. I don't know if the efficiency is not higher than the above method efficiency. There is no problem to solve the problem of more fields. From

http://blog.codelphi.com/progame/archive/2004/02/21/1456.aspxselect rownum, * from MyTable Where Rownum <10 minusselect Rownum, * from MyTable Where Rownum <5

This method should be relatively universal than the above method, and may be implemented in a similar method in SQL Server.

SELECT TOP 10 * from MyTable Minusselect Top 5 * from MyTable

转载请注明原文地址:https://www.9cbs.com/read-17190.html

New Post(0)