本文主要是介绍mysql 分组获取每组某字段最小或最大值记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
mysql 5.8之后分组内排序被优化忽略了;要使用就需要加上 limit
-- 商品列表SELECTspu.spu_id,spu.NAME,sd.image,sd.description,sd.sku_id,sd.store_id,sd.mbr_priceFROMspu spu INNER JOIN (SELECTsku.sku_id,sku.spu_id,sku.image,sku.first_category_id,sku.second_category_id,sku.description,store.store_id,store.mbr_priceFROMsku skuINNER JOIN sku_store_A001 store ON sku.sku_id = store.sku_idAND store.sku_status = 1AND store.mbr_price > 0AND sku.first_category_id = "20200818214223488" ORDER BY sku.spu_id,store.mbr_price limit 1024) sd ON sd.spu_id = spu.spu_id where spu.status = 1 GROUP BY sd.spu_id
有两个最小值相同时,两条记录不会被查询出来
-- 分组取最小select * from sku_gorundby where mbr_price in (select min(mbr_price) from sku_gorundby group by spu_id)
这篇关于mysql 分组获取每组某字段最小或最大值记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!