1.创建表

create table tb(

name varchar(10),

val int,

memo varchar(20)

);

2.插入数据

insert into tb values('a', 2, 'a2(a的第二个值)');

insert into tb values('a', 1, 'a1--a的第一个值');

insert into tb values('a', 3, 'a3:a的第三个值');

insert into tb values('b', 1, 'b1--b的第一个值');

insert into tb values('b', 3, 'b3:b的第三个值');

insert into tb values('b', 2, 'b2b2b2b2');

insert into tb values('b', 4, 'b4b4');

insert into tb values('b', 5, 'b5b5b5b5b5');

3.按name分组取val最大的值所在行的数据

# 方法4:
SELECT a.* FROM tb AS a
INNER JOIN
(SELECT name , max(val) val FROM tb GROUP BY name) AS b
ON a.name = b.name
AND a.val = b.val
ORDER BY a.name ;
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name ;
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val) ;
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name ;
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name ;
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name ;

4.按name分组取val最小的值所在行的数据

--方法1:
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name ;
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val) ;
--方法3:
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name ;
--方法4:
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name ;
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name ;

**5.按name分组取最小的两个(N个)val **

select a.* from tb a
where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2)
order by a.name ;

**6.按name分组取最大的两个(N个)val **

select a.* from tb a
where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2)
order by a.name

创建表

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);

表mygoods为商品表,cat_id为分类id,goods_id为商品id,status为商品当前的状态位(1:有效,0:无效)

sql查询语句

-- 每个分类找出价格最高的两个商品
select a.* from mygoods a
where (select count(*) from mygoods where cat_id = a.cat_id and price > a.price ) <3
order by a.cat_id,a.price desc; -- 每个分类找出价格最高的有效的两个商品(正确)
select a.* from mygoods a
where (select count(*) from mygoods where cat_id = a.cat_id and price > a.price and status=1 ) <3
and status=1
order by a.cat_id, a.price desc ; -- 可以将每个分组下的goods_id合并
select cat_id,GROUP_CONCAT(goods_id) from mygoods group by cat_id; -- 每个分类找出价格最高的商品
select a.* from mygoods a where price = (select max(price) from mygoods where cat_id=a.cat_id) order by a.cat_id; -- 每个分类找出价格最低的商品
select a.* from mygoods a where price = (select min(price) from mygoods where cat_id=a.cat_id) order by a.cat_id;

mysql分组取每组前几条记录(排名)的更多相关文章

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

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

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

    mysql分组取每组前几条记录(排名) 附group by与order by的研究,需要的朋友可以参考下 --按某一字段分组取最大(小)值所在行的数据 复制代码代码如下: /* 数据如下: name ...

  3. Mysql SQL分组取每组前几条记录

    按name分组取最大的两个val: [比当前记录val大的条数]小于2条:即当前记录为为分组中的前两条 > (select count(*) from tb where name = a.nam ...

  4. MYSQL 按某个字段分组,然后取每组前3条记录

    先初始化一些数据,表名为 test ,字段及数据为: SQL执行结果为:每个 uid  都只有 3 条记录.   SQL语句为: SELECT   * FROM   test main WHERE   ...

  5. SQL分组取每组前一(或几)条记录(排名)

    mysql分组取每组前几条记录(排名) 附group by与order by的研究 http://www.jb51.net/article/31590.htm --按某一字段分组取最大(小)值所在行的 ...

  6. mysql查询各种类型的前N条记录

    mysql查询各种类型的前N条记录,将3改为N(需查询条数)即可  (select * from event_info where event_type = 1  limit 3)union all( ...

  7. mysql获取所有分类的前n条记录的两种方法浅析

      项目中有个需求是获取出展会分类下的前n条展商. 刚开始的思路是用group by 获取出展会的分类,后面再根据分类获取该分类下的n个展商,但也需要第一次获取出展会的时候也获取所有的每个展会分类下的 ...

  8. MySQL 分组后取每组前N条数据

    与oracle的 rownumber() over(partition by xxx  order by xxx )语句类似,即:对表分组后排序 创建测试emp表 1 2 3 4 5 6 7 8 9 ...

  9. MySQL 先按某字段分组,再取每组中前N条记录

    按 gpcode每组 取每组 f4 最大的那条记录: 方法一: select * from calcgsdataflash a where gscode = 'LS_F' and ymd >= ...

随机推荐

  1. js----Date\Math\数组对象

  2. 27. Retrofit2 -- How to Use Dynamic Urls for Requests

    27. Retrofit2 -- How to Use Dynamic Urls for Requests Retrofit tutorial 用户案例场景 如何使用动态 Url 相对于基本地址,动态 ...

  3. (译)Getting Started——1.3.1 Incorporating the Data(合并数据)

    在实现完应用的行为后,需要创建数据模型来支撑应用的界面.应用的数据模型定义了应用中数据的维护方法.数据模型可以是一个基本的dictionary类型,也可以是复杂的数据库,范围很广.好的数据模型可以更为 ...

  4. POSTGRESQL 9.1 FATAL: password authentication failed for user "postgres"

    1.配置postgreql 可以远程访问: sudo vim /etc/postgresql/9.1/main/postgresql.conf root@ubuntuserver:~# sudo vi ...

  5. JS——简单的正则表达式验证

    <!-- 用户注册:结构层:html;表现层:css;行为层:javascript; html利用ul,li来构造: 注意事项:1.每个Input都要有相应的id,这是在js中去调用的. 2.& ...

  6. php调用c语言编写的so动态库

    from http://blog.csdn.net/wzhwho/article/details/6949297 PHP除了使用扩展库的方式调用c函数,还可以通过socket通信的方式.这里介绍前者. ...

  7. 逃不掉的mysql数据库安装方式大全yum rpm 源码

    数据库虽然也不是天天安装,但每次安装都要找来找去挺烦,特整理记录在此. 系统基于:Centos 7.x 数据库版本: MySQL 5.7.x 转载请注明出处 Yum 安装方式 1.下载 yum rep ...

  8. Using Fast Weights to Attend to the Recent Past

    Ba, Jimmy, et al. "Using Fast Weights to Attend to the Recent Past." Advances In Neural In ...

  9. 如何将HTML页面的标题设置为“数字天堂”。

    如何将HTML页面的标题设置为“数字天堂”. 解答: <html> <head><title>数字天堂</title></head> < ...

  10. noip 模拟赛 After 17(递推+特殊的技巧)

    来源:Violet_II T1 好神的一题,我竟然没做出来QAQ 首先我们发现,答案是sigma(x[i]*x[j], i>j)+sigma(y[i]*y[j], i>j).显然只需要讨论 ...