如何在MS SQL中限制和排序?

时间:2022-09-22 00:19:17

I'm familiar with MySQL but not in MS SQL,

我熟悉MySQL但不熟悉MS SQL,

In MySQL the query will be like this:

在MySQL中,查询将如下所示:

SELECT * from tablename ORDER BY RAND() LIMIT 5

The above query gives a 5 random number of result from the table. The same query how can i do with MS SQL?

上面的查询给出了表中5个随机数的结果。同样的查询如何处理MS SQL?

3 个解决方案

#1


11  

Try,

尝试,

SELECT TOP 5 * from tablename ORDER BY NEWID()

#2


3  

Have a look at using TOP and order by NEWID

看看使用TOP和NEWID订购

Something like

就像是

SELECT TOP 5 *
FROM TableName 
ORDER BY NEWID()

#3


1  

This would be:

这将是:

SELECT TOP 5 * FROM tablename
ORDER BY NEWID()

Tested in MSSQL 2005.

在MSSQL 2005中测试过。

#1


11  

Try,

尝试,

SELECT TOP 5 * from tablename ORDER BY NEWID()

#2


3  

Have a look at using TOP and order by NEWID

看看使用TOP和NEWID订购

Something like

就像是

SELECT TOP 5 *
FROM TableName 
ORDER BY NEWID()

#3


1  

This would be:

这将是:

SELECT TOP 5 * FROM tablename
ORDER BY NEWID()

Tested in MSSQL 2005.

在MSSQL 2005中测试过。