【发布时间】:2015-10-19 18:12:34
【问题描述】:
我需要从表中随机获取 3 行,然后按 BannerWeight 列对这些行进行排序。
所以如果数据是:
BannerID BannerWeight
1 5
2 5
3 10
4 5
5 10
我希望结果是:
BannerID BannerWeight
5 10
2 5
4 5
到目前为止我有:
SELECT TOP 3 b.BannerID, b.BannerWeight FROM CMS_Banner b
INNER JOIN CMS_BannerCategory c ON b.BannerCategoryID = c.BannerCategoryID
WHERE c.BannerCategoryName LIKE 'HomepageSponsors'
ORDER BY NEWID()
我只是不知道如何订购这 3 个随机行。我试过做
ORDER BY BannerWeight, NEWID()
但这只会让我得到 3 个随机行,其中 BannerWeight 为 5。
这是一个 SQLFiddle:http://sqlfiddle.com/#!6/a8088/2/0
【问题讨论】:
标签: sql sql-server select random