Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groupby mysql在使用排序时显示不同的记录_Mysql_Group By_Greatest N Per Group - Fatal编程技术网

Groupby mysql在使用排序时显示不同的记录

Groupby mysql在使用排序时显示不同的记录,mysql,group-by,greatest-n-per-group,Mysql,Group By,Greatest N Per Group,当我使用下面的sql运行查询时 SELECT userproduct_id,product_id,offer_price FROM `tbluserproducts` WHERE `product_id` = '4722' ORDER BY `offer_price` asc 结果是这样的 userproduct_id product_id offer_price 4848 4722 1200 4835 4722 1

当我使用下面的sql运行查询时

SELECT 
userproduct_id,product_id,offer_price
FROM `tbluserproducts`
WHERE `product_id` = '4722'
ORDER BY `offer_price` asc
结果是这样的

userproduct_id  product_id  offer_price
4848            4722        1200
4835            4722        12500
4837            4722        12500
4841            4722        17000
当在上面的sql中添加groupby product_id时

SELECT 
userproduct_id,product_id,offer_price
FROM `tbluserproducts`
WHERE `product_id` = '4722'
group by product_id
ORDER BY `offer_price` asc
它只显示一条记录

userproduct_id  product_id  offer_price
4835            4722        12500
但它没有显示userproduct_id 4848记录,因为我是按asc的报价订购的

我的表格结构如下

Column          Type                 
userproduct_id  int(10) unsigned Auto Increment 
product_id      int(11) unsigned    
offer_price     decimal(30,0)
你可以在下面试试-

    SELECT 
    userproduct_id,product_id,offer_price
    FROM `tbluserproducts`
    WHERE `product_id` = '4722' and userproduct_id=(select max(userproduct_id)
from `tbluserproducts` b where b.`product_id` = '4722')

使用GROUP BY时,查询将返回一行product_id 4722。如果组中有多行,MySQL将从该行集中选择一行

它如何选择行

它必须以某种顺序读取行,并选择它读取的第一行。它在此查询中按主键顺序读取行,因此它显示具有最少userproduct_id的行

您给出的顺序在GROUP BY将输出减少到一行后应用,因此它对一组一行进行排序,而这没有任何效果

我想你想退回报价最低的那一行,对吗?你可以在你的问题中更清楚地说明这一点

这基本上是相同类型的问题,已经被问了几百次堆栈溢出。标记用于此一般类型的问题

在查询中,您只选择了一个product_id,因此使用LIMIT只返回第一行很容易

SELECT 
  userproduct_id, product_id, offer_price
FROM `tbluserproducts`
WHERE `product_id` = '4722'
ORDER BY `offer_price` asc
LIMIT 1
如果你想要很多产品id,并且每一个都有最低报价的那一行,这就有点复杂了。在MySQL 8.0中,您可以使用窗口功能:

WITH ranked_userproducts AS (
  SELECT 
    userproduct_id, product_id, offer_price,
    ROW_NUMBER() OVER (PARTITION BY product_id ORDER BY offer_price) AS rownum
  FROM `tbluserproducts`
)
SELECT 
  userproduct_id, product_id, offer_price
FROM ranked_userproducts
WHERE rownum = 1;
在MySQL 5.x中,您可以尝试另一种方法:

SELECT 
  userproduct_id, product_id, offer_price
FROM `tbluserproducts` AS p1
JOIN (
  SELECT product_id, MIN(offer_price) AS offer_price
  GROUP BY product_id ORDER BY NULL
) AS p2
  ON p1.product_id = p2.product_id AND p1.offer_price = p2.offer_price;

也许还有其他解决办法。我建议您按照标记阅读其他答案。

GROUP BY查询中任何未聚合的列或GROUP BY子句中的列都将采用随机值,而不管ORDER BY子句如何。@Nick,不是随机行,因为它是可重复的。按索引顺序,它基本上是MySQL在组中读取的第一行。但这是依赖于实现的,并且没有保证。@Nick如何aggregated@BillKarwin你能不能给我一个更好的术语,也就是说,一个词意味着你得到的值是随机的,但你每次尝试都会得到相同的值?@Pritamkumar函数,如SUM,COUNT,MIN,MAX等。我使用了您的查询,它显示的记录类似于此userproduct\u id product\u id offer\u price 4848 4722 1200 4835 4722 12500 4837 4722 12500 4841 472217000@Pritamkumar,比如说什么?@Pritamkumar,那么你的预期输出是什么呢?用户产品标识产品标识报价4848 4722 1200谢谢你解决了我的问题我试着用它工作选择用户产品标识产品标识,来自tbluserproducts的报价,其中product_id='4722'和PROFERE_price=从tbluserproducts b中选择minoffer_价格,其中b.product_id='4722'