简体   繁体   English

sql Query选择每个引用的三个id

[英]sql Query to select three of each referenced id

I'm using a SqlDataSource to bring back data to be displayed in a drop down. 我正在使用SqlDataSource来恢复要在下拉列表中显示的数据。 The headers of each drop down is the brand and in the drop down I want three products of each brand to be brought back. 每个下拉列表的标题是品牌,在下拉列表中我希望每个品牌的三个产品被带回来。

<asp:SqlDataSource ID="Categories" runat="server" ConnectionString="<%$ ConnectionStrings:Categories %>" SelectCommand="SELECT * FROM CATEGORY WHERE CategoryID <5"></asp:SqlDataSource>
<asp:ListView ID="ListView2" runat="server" DataSourceID="Categories">
<LayoutTemplate>
    <ul>
    <asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
    <asp:SqlDataSource ID="images" runat="server" ConnectionString="<%$ ConnectionStrings:Categories %>" SelectCommand="SELECT * FROM ITEM ORDER BY Category_ref"></asp:SqlDataSource>
    <li id="<%# Eval("CategoryName") %>">
    <a href="<%# Eval("CategoryName", "{0}.aspx") %>"><%# Eval("CategoryName") %></a>
    <div class="dropDown">
        <div class="listItem">
        <asp:ListView ID="listview3" runat="server" DataSourceID="images">
            <LayoutTemplate>
            <asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
            </LayoutTemplate>
            <ItemTemplate>
            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("ItemID","Product.aspx?id={0}") %>'>
                <p class="<%# Eval("Category_ref", "brand{0}") %>">
                <asp:Image ID="image" CssClass="dropImg" runat="server" ImageUrl='<%# Eval("PhotoPath", "images/products/{0}") %>' />
                <br />
                <span><%# Eval("ItemName") %></span>

                <%# Eval("ItemID").ToString() %>
                </p>
            </asp:HyperLink>
            </ItemTemplate>
        </asp:ListView>
        </div>
    </div>
    </li>
</ItemTemplate>
</asp:ListView>

That is my entire navigation with the brand names coming back from database as well as the product information. 这是我的整个导航,其中包含从数据库返回的品牌名称以及产品信息。

The second SqlDataSource query is where I need to fix up so it only selects 3 product. 第二个SqlDataSource查询是我需要修复的地方,所以它只选择3个产品。 If there is a fourth it will leave it out. 如果有第四个,它会将其遗漏。

In SQL Server: 在SQL Server中:

SELECT TOP 3 * FROM ITEM ORDER BY Category_ref

In MySQL: 在MySQL中:

SELECT * FROM ITEM ORDER BY Category_ref LIMIT 3

You should always specify your database of choice with your question, and reduce the problem to the essence (all that ASP code is irrelevant and just scares people from your question). 您应该始终使用您的问题指定您选择的数据库,并将问题简化为最重要的(所有ASP代码都无关紧要,只会让人们对您的问题感到害怕)。

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

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