如何在列ArticleViews中输出具有最高值的前5个结果

时间:2014-05-14 14:00:58

标签: c# sql select

以下是我的数据来源。

<asp:SqlDataSource ID="getbyviews" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectAntiFrack %>" 
SelectCommand="SELECT [ArticleID], [ArticleTitle], [ArticlePublished], [ArticleViews] 
FROM [ArticlesTBL] 
WHERE ([ArticlePublished] = @ArticlePublished) 
ORDER BY [ArticleViews] DESC">
<SelectParameters>
<asp:Parameter DefaultValue="True" Name="ArticlePublished" Type="Boolean" />
</SelectParameters>
</asp:SqlDataSource>

有没有办法操纵select语句只能获得5个最高结果?或者这可以在C#中完成吗? 我的数据源将在稍后用于填充列表视图。

1 个答案:

答案 0 :(得分:3)

只需使用TOP 5

SELECT TOP 5 [ArticleID], [ArticleTitle], [ArticlePublished], [ArticleViews] 
FROM [ArticlesTBL] 
WHERE ([ArticlePublished] = @ArticlePublished) 
ORDER BY [ArticleViews] DESC