I have a little SQL problem.
I want to select 5 random rows of the 20 rows with highest ID
. How do I do that? At the moment my SqlDataSource
just looks like this:
<asp:SqlDataSource ID="SqlDataSource25" runat="server"
ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
SelectCommand="SELECT TOP 5 * FROM [billeder] ORDER BY newid()">
</asp:SqlDataSource>
which means I of course just gets 5 random from the entire table.
I would prefer to do it via the SqlDataSource
as you can see here, but if it is only possible via codebehind that is fine aswell. Any tips would be appreciated.
You need to use a subquery to get the top 20 by ID, then from this 20 you can select the top 5:
SELECT TOP 5 *
FROM ( SELECT TOP 20 *
FROM [billeder]
ORDER BY ID DESC
) AS t
ORDER BY NEWID();
select * from [billeder] where [yourPk] in
(select top 5 percent [yourPk] from [billeder] order by newid())
yourPk is primarykey
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.