简体   繁体   中英

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. 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. If there is a fourth it will leave it out.

In SQL Server:

SELECT TOP 3 * FROM ITEM ORDER BY Category_ref

In 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).

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.

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