如何在不排序的情况下选择表格中的前10行? [重复]

时间:2021-12-24 09:22:18

Possible Duplicate:
How do I select last 5 rows in a table without sorting?

可能重复:如何在不排序的情况下选择表中的最后5行?

I want to select the top 10 records from a table in SQL Server without arranging the table in ascending or descending order.

我想从SQL Server中的表中选择前10条记录,而不按升序或降序排列表。

3 个解决方案

#1


11  

if random order is needed, you can try

如果需要随机订单,您可以尝试

select top 10 * from [tablename] order by newid()

#2


8  

The question does not make sense. In SQL a table has no implicit ordering. They may as well be returned in random order, semantically speaking.

这个问题没有意义。在SQL中,表没有隐式排序。从语义上讲,它们也可以以随机顺序返回。

Limiting the results to the first 10 rows returned depends on your server's SQL dialect. In MS-SQL server, you use the TOP keyword, while in MySQL you use LIMIT, in Oracle you have to use ROWNUM, etc.

将结果限制为返回的前10行取决于服务器的SQL方言。在MS-SQL服务器中,您使用TOP关键字,而在MySQL中使用LIMIT,在Oracle中您必须使用ROWNUM等。

Please provide more details on what exactly you are trying to accomplish.

请提供有关您要完成的具体内容的更多详细信息。

#3


4  

SELECT TOP 10 <requiredfieldListHere> FROM <TheTableNameHere>

SELECT TOP 10 FROM

If you have clustered index this will return the first 10 records in the table. Note however that this would be bad form. A relational table should not be considered as having any particular order. If you don't have a clustered index, it may to return the first 10 records but could just easily return a random set.

如果您有聚簇索引,这将返回表中的前10条记录。但请注意,这将是糟糕的形式。关系表不应被视为具有任何特定顺序。如果您没有聚集索引,它可能会返回前10个记录,但可以轻松返回随机集。

Unless you are happy for this to return any 10 records from the table, you should apply a an ORDER BY to your query. This is true even if you have a clustered index since it may be removed or changed in the future.

除非您乐于从表中返回任何10条记录,否则您应该在查询中应用ORDER BY。即使您有聚簇索引也是如此,因为将来可能会删除或更改它。

#1


11  

if random order is needed, you can try

如果需要随机订单,您可以尝试

select top 10 * from [tablename] order by newid()

#2


8  

The question does not make sense. In SQL a table has no implicit ordering. They may as well be returned in random order, semantically speaking.

这个问题没有意义。在SQL中,表没有隐式排序。从语义上讲,它们也可以以随机顺序返回。

Limiting the results to the first 10 rows returned depends on your server's SQL dialect. In MS-SQL server, you use the TOP keyword, while in MySQL you use LIMIT, in Oracle you have to use ROWNUM, etc.

将结果限制为返回的前10行取决于服务器的SQL方言。在MS-SQL服务器中,您使用TOP关键字,而在MySQL中使用LIMIT,在Oracle中您必须使用ROWNUM等。

Please provide more details on what exactly you are trying to accomplish.

请提供有关您要完成的具体内容的更多详细信息。

#3


4  

SELECT TOP 10 <requiredfieldListHere> FROM <TheTableNameHere>

SELECT TOP 10 FROM

If you have clustered index this will return the first 10 records in the table. Note however that this would be bad form. A relational table should not be considered as having any particular order. If you don't have a clustered index, it may to return the first 10 records but could just easily return a random set.

如果您有聚簇索引,这将返回表中的前10条记录。但请注意,这将是糟糕的形式。关系表不应被视为具有任何特定顺序。如果您没有聚集索引,它可能会返回前10个记录,但可以轻松返回随机集。

Unless you are happy for this to return any 10 records from the table, you should apply a an ORDER BY to your query. This is true even if you have a clustered index since it may be removed or changed in the future.

除非您乐于从表中返回任何10条记录,否则您应该在查询中应用ORDER BY。即使您有聚簇索引也是如此,因为将来可能会删除或更改它。