简体   繁体   English

随机选择并对记录进行排序

[英]Select randomly And Sort the records

I have a table which has ID,name and Level columns.I want to SELECT the records of the Table by this pattern : First Select Them randomly and then sort those random records by level column. 我有一个包含ID,名称和级别列的表。我想按这种模式选择表的记录:首先随机选择它们,然后按级别列对这些随机记录进行排序。

for example : my sample table and records: 例如:我的样本表和记录:

ID      name         Level
--------------------------------- 
1      red-book         1
2      blue-pen         10
3      black-board      12
4      balck-Book       1
5      white-book       1
6      red-pen          10
7      green-pen        10

And the result should be something like this : 结果应该是这样的:

ID            name             level
------------------------------------------
3             black-board      12
6             red-pen          10
2             blue-pen         10
7             green-pen        10
4             balck-Book       1
1             red-book         1
5             white-book       1

I've also used 我也用过

 SELECT * FROM MyTable ORDER BY NEWID(),Level DESC

And

 SELECT * FROM 
 (SELECT * FROM MyTable ORDERBY NEWID())As TempTbl 
 ORDER BY Level DESC

And

 CREATE TABLE #MyTempTable (ID INT,name Nvarchar(256),Levels INT)

 INSERT INTO #MyTempTable SELECT * FROM MyTable ORDER BY NEWID()

 SELECT * FROM #MyTempTable ORDER BY Levels DESC
SELECT ID,name,level
FROM sample
ORDER BY level DESC,NEWID()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM