首先来造一部分数据,表mygoods为商品表,cat_id为分类id,goods_id为商品id,status为商品当前的状态位(1:有效,0:无效)。

CREATE TABLE `mygoods` (  `goods_id` int(11) unsigned NOT NULL AUTO_INCREMENT,  `cat_id` int(11) NOT NULL DEFAULT '0',  `price` tinyint(3) NOT NULL DEFAULT '0',  `status` tinyint(3) DEFAULT '1',  PRIMARY KEY (`goods_id`),  KEY `icatid` (`cat_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;  INSERT INTO `mygoods` VALUES (1, 101, 90, 0);
INSERT INTO `mygoods` VALUES (2, 101, 99, 1);
INSERT INTO `mygoods` VALUES (3, 102, 98, 0);
INSERT INTO `mygoods` VALUES (4, 103, 96, 0);
INSERT INTO `mygoods` VALUES (5, 102, 95, 0);
INSERT INTO `mygoods` VALUES (6, 102, 94, 1);
INSERT INTO `mygoods` VALUES (7, 102, 93, 1);
INSERT INTO `mygoods` VALUES (8, 103, 99, 1);
INSERT INTO `mygoods` VALUES (9, 103, 98, 1);
INSERT INTO `mygoods` VALUES (10, 103, 97, 1);
INSERT INTO `mygoods` VALUES (11, 104, 96, 1);
INSERT INTO `mygoods` VALUES (12, 104, 95, 1);
INSERT INTO `mygoods` VALUES (13, 104, 94, 1);
INSERT INTO `mygoods` VALUES (15, 101, 92, 1);
INSERT INTO `mygoods` VALUES (16, 101, 93, 1);
INSERT INTO `mygoods` VALUES (17, 101, 94, 0);
INSERT INTO `mygoods` VALUES (18, 102, 99, 1);
INSERT INTO `mygoods` VALUES (19, 105, 85, 1);
INSERT INTO `mygoods` VALUES (20, 105, 89, 0);
INSERT INTO `mygoods` VALUES (21, 105, 99, 1);  

需求:每个分类下,找出两个价格最高的有效的商品。

1. 每个分类找出价格最高的两个商品

mysql> select a.*   -> from mygoods a   -> where (select count(*) -> from mygoods -> where cat_id = a.cat_id and price > a.price  ) <2 -> order by a.cat_id,a.price desc;
+----------+--------+-------+--------+
| goods_id | cat_id | price | status |
+----------+--------+-------+--------+
|        2 |    101 |    99 |      1 |
|       17 |    101 |    94 |      0 |
|       18 |    102 |    99 |      1 |
|        3 |    102 |    98 |      0 |
|        8 |    103 |    99 |      1 |
|        9 |    103 |    98 |      1 |
|       11 |    104 |    96 |      1 |
|       12 |    104 |    95 |      1 |
|       21 |    105 |    99 |      1 |
|       20 |    105 |    89 |      0 |
+----------+--------+-------+--------+
10 rows in set (0.00 sec)

2. 每个分类找出价格最高的有效的两个商品(正确)

mysql> select a.* -> from mygoods a -> where (select count(*) from mygoods -> where cat_id = a.cat_id and price > a.price and status=1  ) <2 -> and status=1 -> order by a.cat_id,a.price desc ;
+----------+--------+-------+--------+
| goods_id | cat_id | price | status |
+----------+--------+-------+--------+
|        2 |    101 |    99 |      1 |
|       16 |    101 |    93 |      1 |
|       18 |    102 |    99 |      1 |
|        6 |    102 |    94 |      1 |
|        8 |    103 |    99 |      1 |
|        9 |    103 |    98 |      1 |
|       11 |    104 |    96 |      1 |
|       12 |    104 |    95 |      1 |
|       21 |    105 |    99 |      1 |
|       19 |    105 |    85 |      1 |
+----------+--------+-------+--------+
10 rows in set (0.00 sec)

3. 每个分类找出价格最高的有效的两个商品(正确)

mysql> select a.* -> from mygoods a -> left join mygoods b -> on a.cat_id = b.cat_id and a.price < b.price and b.status=1-> where a.status=1-> group by a.goods_id,a.cat_id,a.price-> having count(b.goods_id) < 2-> order by a.cat_id,a.price desc;
+----------+--------+-------+--------+
| goods_id | cat_id | price | status |
+----------+--------+-------+--------+
|        2 |    101 |    99 |      1 |
|       16 |    101 |    93 |      1 |
|       18 |    102 |    99 |      1 |
|        6 |    102 |    94 |      1 |
|        8 |    103 |    99 |      1 |
|        9 |    103 |    98 |      1 |
|       11 |    104 |    96 |      1 |
|       12 |    104 |    95 |      1 |
|       21 |    105 |    99 |      1 |
|       19 |    105 |    85 |      1 |
+----------+--------+-------+--------+
10 rows in set (0.00 sec)

4.每个分类找出价格最高的有效的两个商品(错误)
mysql> select a.* -> from mygoods a -> where (select count(*) from mygoods -> where cat_id = a.cat_id and price > a.price  ) <2 and status=1 -> order by a.cat_id,a.price desc;
+----------+--------+-------+--------+
| goods_id | cat_id | price | status |
+----------+--------+-------+--------+
|        2 |    101 |    99 |      1 |
|       18 |    102 |    99 |      1 |
|        8 |    103 |    99 |      1 |
|        9 |    103 |    98 |      1 |
|       11 |    104 |    96 |      1 |
|       12 |    104 |    95 |      1 |
|       21 |    105 |    99 |      1 |
+----------+--------+-------+--------+
7 rows in set (0.00 sec)

由上可知,如果需要增加条件的话,需要在两处增加条件。

可以将每个分组下的goods_id合并。

mysql> select cat_id,GROUP_CONCAT(goods_id) from mygoods group by cat_id;
+--------+------------------------+
| cat_id | GROUP_CONCAT(goods_id) |
+--------+------------------------+
|    101 | 1,2,15,16,17           |
|    102 | 3,5,6,7,18             |
|    103 | 4,8,9,10               |
|    104 | 11,12,13               |
|    105 | 19,20,21               |
+--------+------------------------+
5 rows in set (0.00 sec)

转载于:https://www.cnblogs.com/duhuo/p/4385642.html

mysql分组取每组前几条记录(排序)相关推荐

  1. mysql 排序取前4,mysql分组取每组前几条记录(排序)

    首先来造一部分数据,表mygoods为商品表,cat_id为分类id,goods_id为商品id,status为商品当前的状态位(1:有效,0:无效). CREATE TABLE `mygoods` ...

  2. mysql单列去重复group by分组取每组前几条记录加order by排序

    <div class="post"><h1 class="postTitle"><a id="cb_post_title ...

  3. mysql自连接分组查询最新_MySQL 自连接分组取每组最大N条记录

    1.测试数据: create table t2 ( id int primary key, gid char, col1 int, col2 int ) engine=myisam; insert i ...

  4. SQL之Join的使用详解(附 :分组查询每组前N条记录)

    一.基本概念 关于sql语句中的连接(join)关键字,是较为常用而又不太容易理解的关键字,下面这个例子给出了一个简单的解释 –建表user1,user2: table1 : create table ...

  5. sql 取表的前10条记录,任意中间几行的记录

    取表的前10条记录 with a as(select *,row_number()over(order by department)rn from _SucceedStaff ) select * f ...

  6. mysql分组取出每组地一条数据_基于mysql实现group by取各分组最新一条数据

    基于mysql实现group by取各分组最新一条数据 前言: group by函数后取到的是分组中的第一条数据,但是我们有时候需要取出各分组的最新一条,该怎么实现呢? 本文提供两种实现方式. 一.准 ...

  7. mysql分组取出每组地一条数据_MYSQL实现分组排序并取组内第一条数据

    一.需要实现分组排序并且取组内状态优先级最高的数据 有一张这样的数据表, 需求是根据error_type分组然后取status最小的第一条数据 第一种写法: select t.* from ( sel ...

  8. 分组取每组的第一条或前N条

    情景:每天收集到很多意思相近的新闻,按照时间,取每天的第一条 1.group by 按照时间分组 select distinct substring(pubTime,1,10) pubTime,max ...

  9. MongoDB分组取每组中一条数据

    需求背景 有一个mongo collection,里面存放了运送货物的司机位置信息,字段主要有 _id: mongodb默认的主键字段 orderId:订单id positionTime:位置上报时的 ...

最新文章

  1. JavaScript原型-进阶者指南
  2. MATLAB 表数据结构最终篇,如何实现表操作
  3. 数组中子数组求最大和
  4. 1-Dimensional Heightfield Visibility Query
  5. 《Learning.Python》pdf
  6. The ntpath module
  7. SQL连接查询深度探险
  8. Java JVM总结
  9. sqoop导数据出现问题
  10. mysql主从虚拟机_虚拟机centos7Mysql实现主从配置
  11. 每日一题(45)—— 字符数组找错
  12. mysql怎么显示创表的语句_141张图带你 MySQL 入门
  13. 大华的支持rtmp推流吗_RTSP安防摄像机(海康大华宇视等)如何推送到RTMP流媒体服务器进行直播...
  14. opencv mat赋值_【3】OpenCV图像处理模块(18)重映射
  15. SQL Server 2000/2005/2008 系列产品下载地址
  16. 软件测试基础理论全集
  17. 值得你收藏的Notes应用模板
  18. php aes加密 ctr,使用aes+ctr的加密问题
  19. 其次坐标,以及和非其次坐标互转
  20. 【图像超分辨率重建】——EnhanceNet论文精读笔记

热门文章

  1. 开公司的两个方向,要么把公司开成很赚钱,要么把公司做成很值钱
  2. 什么样的领导最有魅力?
  3. 京东也准备向社区团购进发了?
  4. 一切想要发财的人,你都要善于看到隐形的东西
  5. 人到中年,别再挥霍你的“同情心”了
  6. Vue:开发者友好性和易用性
  7. 如何评价NVIDIA RTX 2080 Ti显卡?
  8. 安装Windows 10 V1909对CPU有什么要求?
  9. 3.3设计自己的线程局部存储
  10. uni-app中view组件的基本使用