Inner Join table with respect to a maximum value

后端 未结 1 950
夕颜
夕颜 2021-01-12 04:15

I\'m trying to write a MySQL query where I pull a seller\'s info and her most popular product. This is determined by the product with the most page views, i.e. MAX(pa

相关标签:
1条回答
  • 2021-01-12 05:02
    SELECT  a.ID SellerID,
            a.Shop_Name,
            b.ID ProductID,
            b.pageViews,
            b.title,
            b.Price
    FROM    seller a
            INNER JOIN Products b
                ON a.id = b.seller_ID
            INNER JOIN
            (
                SELECT  seller_ID, MAX(pageViews) max_view
                FROM    products
                GROUP   BY seller_ID
            ) c ON  b.seller_ID = c.seller_ID AND
                    b.pageViews = c.max_View
    WHERE   a.handpicked = 'Y' AND a.active = 'Y'
    
    • SQLFiddle Demo

    OUTPUT

    ╔══════════╦═══════════╦═══════════╦═══════════╦═════════╦═══════╗
    ║ SELLERID ║ SHOP_NAME ║ PRODUCTID ║ PAGEVIEWS ║  TITLE  ║ PRICE ║
    ╠══════════╬═══════════╬═══════════╬═══════════╬═════════╬═══════╣
    ║        1 ║ mitienda  ║         2 ║        30 ║ bufanda ║ $25   ║
    ║        3 ║ new_world ║         6 ║         6 ║ ropa    ║ $13   ║
    ╚══════════╩═══════════╩═══════════╩═══════════╩═════════╩═══════╝
    
    0 讨论(0)
提交回复
热议问题