内连接表的最大值 [英] Inner Join table with respect to a maximum value

查看:69
本文介绍了内连接表的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个MySQL查询,在其中提取卖方的信息和她最受欢迎的产品.这由浏览量最多的产品确定,即MAX(page_views).

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(page_views).

以下查询只是拉出随机产品,而不是浏览量最多的产品.

The query below though is just pulling a random product and not the one with the most page views.

"SELECT 
     seller.id, seller.language, seller.shop_name,seller.story, 
     seller.eng_story, product.id, product.image_thumb, product.title, 
     product.eng_title, product.price, MAX(product.page_views) 
  FROM seller 
     INNER JOIN product ON seller.id=product.seller_id 
  WHERE seller.handpicked='y' AND seller.shop_active='y' 
  GROUP BY seller.id 
  ORDER BY product.page_views
  LIMIT 0,5"

或者更好的说,page_views实际上是正确的数字,但是我如何获得与产品相关的其他产品字段(ID,图片,标题等)拥有最多的页面浏览量.

Or better said, page_views is in fact the correct number, but how do I get the other product fields (id, image, title, etc.) to be with respect to the product with the most page views.

表格数据:

卖家:

id | language | shop_Name | story     | eng_story   | handpicked | active
1  |   1      | mitienda  | hola mundo| Hello world | Y          | Y
2  |   1      | sisenor   | bonita    | beautiful   | N          | Y
3  |   2      | new_world | mi vida   | my life     | Y          | Y

产品:

id | seller_id | image_thumb | title    | eng_title | price | page Views
1  |  1        | /images/..  | sombrero | hat       | $5    | 10
2  |  1        | /images/..  | bufanda  | scarf     | $25   | 30
3  |  2        | /images/..  | arte     | art       | $15   | 15
4  |  3        | /images/..  | joyeria  | jewlery   | $10   | 1
5  |  2        | /images/..  | canasta  | basket    | $21   | 13
6  |  3        | /images/..  | ropa     | clothes   | $13   | 6

预期结果(精简):

seller.id | shop_name | product.id | pageviews | title    | price
  1       |  miteinda |     2      |    30     |  bufanda | $25
  3       |  newworld |     6      |     6     |  ropa    | $13

结果应按浏览量列出经过精心挑选的卖家信息及其最受欢迎的产品.按浏览量对卖家进行排序,总共限制为5个卖家.

The result should list out sellers information who have been handpicked and their most popular product by pageviews The sellers are ordered by pageviews with a limit of 5 sellers total.

推荐答案

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演示
    • SQLFiddle Demo
    • 输出

      ╔══════════╦═══════════╦═══════════╦═══════════╦═════════╦═══════╗
      ║ SELLERID ║ SHOP_NAME ║ PRODUCTID ║ PAGEVIEWS ║  TITLE  ║ PRICE ║
      ╠══════════╬═══════════╬═══════════╬═══════════╬═════════╬═══════╣
      ║        1 ║ mitienda  ║         2 ║        30 ║ bufanda ║ $25   ║
      ║        3 ║ new_world ║         6 ║         6 ║ ropa    ║ $13   ║
      ╚══════════╩═══════════╩═══════════╩═══════════╩═════════╩═══════╝
      

      这篇关于内连接表的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆