gpt4 book ai didi

mysql - 关于最大值的内连接表

转载 作者:可可西里 更新时间:2023-11-01 06:40:10 25 4
gpt4 key购买 nike

我正在尝试编写一个 MySQL 查询,从中提取卖家的信息和她最受欢迎的产品。这是由具有最多页面浏览量的产品决定的,即 MAX(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、图片、标题等)关于页面浏览量最多的产品。

表格数据:

卖家:

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 个卖家。

最佳答案

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'

输出

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

关于mysql - 关于最大值的内连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15402025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com