I believe that many SQL statements have been used by many people. The usage of TOP statements is: SELECTTOP (n) FROMtablenameWHEREcondition. We can easily go from the tablename table to the first n data entries that meet the conditions. Bytes --------------------------------------------------------------------------------------
I believe that many SQL statements have been used by many people. The use of the TOP statement is: SELECT TOP (n) FROM tablename WHERE condition. We can easily go FROM the tablename table to the first n pieces of data that meet the conditions. Bytes --------------------------------------------------------------------------------------
I believe many SQL statements have been used by many people.
The usage is as follows:
Select top (n)
FROM tablename
WHERE condition
We can easily go from the tablename table to the first n data entries that meet the conditions.
Bytes --------------------------------------------------------------------------------------
For example, we have tables
Aaa
The data in the table is:
Qq qq2
A1 11
A2 11
A3 11
A4 11
Run:
Select top (2) qq, qq2
FROMaaa
The result is:
Qqqq2
A1 11
A2 11
This is exactly what we want. It is really good and convenient to use.
Bytes --------------------------------------------------------------------------------------
However, today, a program runs out and finds that it is not what you want.
This is the case,
The data is still the data above. I added an order by statement for the Search Condition above the search condition. Of course, this orderby is a bit special, that is, the values of all columns to be sorted are the same.
If the TOP condition is normal
SELECT qq, qq2
FROMaaa
ORDER BYqq2
The result is the same as the order in which no orderby is stored in the database.
Qq qq2
A1 11
A2 11
A3 11
A4 11
When we add a TOP (1) instance, normally, Hong Kong virtual hosts and virtual hosts should be retrieved from the first server in the United States, that is, [a1] [11 ].
Select top (1) qq, qq2
FROMaaa
ORDER BYqq2
However, the data actually retrieved is as follows:
Qqqq2
A2 11
At this time, I feel that SQL Server is a bit different from our common sense.
Similarly, when we change to TOP (2), the third and second pieces are retrieved.
In TOP (3), the fourth, third, and second pieces are retrieved.
SQL Server may not be able to do this because the values of the sorted fields are the same and equivalent data can be considered. The returned data has no impact on the business.
* In programming, we may occasionally encounter unexpected things and surprise us.