mysql 分组排序取最值
- 查各个用户下单最早的一条记录
- 查各个用户下单最早的前两条记录
- 查各个用户第二次下单的记录
一、建表填数据:
SET NAMES utf8mb4;
-- 取消外键约束
SET FOREIGN_KEY_CHECKS = 0; -- ----------------------------
-- Table structure for order_table
-- ----------------------------
DROP TABLE IF EXISTS `order_table`;
CREATE TABLE `order_table` (
`order_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`create_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; -- ----------------------------
-- Records of order_table
-- ----------------------------
BEGIN;
INSERT INTO `order_table` VALUES (1, 1, '2019-03-03 16:29:58');
INSERT INTO `order_table` VALUES (2, 1, '2019-03-04 16:30:17');
INSERT INTO `order_table` VALUES (3, 1, '2019-03-05 16:30:29');
INSERT INTO `order_table` VALUES (4, 2, '2019-03-03 16:35:23');
INSERT INTO `order_table` VALUES (5, 2, '2019-03-04 16:35:34');
COMMIT;
-- 添加外键约束
SET FOREIGN_KEY_CHECKS = 1;
二、解决上述三个问题
1、查各个用户下单最早的一条记录
select ot.* FROM order_table ot
where create_time = (
SELECT min(create_time) FROM order_table where user_id = ot.user_id
) order by ot.user_id
结果:

2、查各个用户下单最早的前两条记录
-- 分组最小的前两条
select * from order_table ot
where 2 > (select count(*) from order_table where user_id = ot.user_id and create_time < ot.create_time )
order by ot.user_id, ot.create_time
结果:

3、查各个用户第二次下单的记录
-- 分组排序,取create_time倒数第二小的数据
select * FROM (
-- 按user_id分组、create_time asc排序、取create_time最小和倒数第二小的值
select * from order_table oot
where 2 > (
select count(*)
from order_table
where user_id = oot.user_id and create_time < oot.create_time
)
order by oot.user_id, oot.create_time
) as ttt where
ttt.order_id not in
( -- 按user_id分组、create_time asc排序、取create_time最小的值
select ot.order_id FROM order_table ot
where create_time = (
SELECT min(create_time) FROM order_table where user_id = ot.user_id
) order by ot.user_id
)
结果:

参考链接:http://www.manongjc.com/article/1082.html
mysql 分组排序取最值的更多相关文章
- mysql分组排序取最大值所在行,类似hive中row_number() over partition by
如下图, 计划实现 :按照 parent_code 分组, 取组中code最大值所在的整条记录,如红色部分.(类似hive中: row_number() over(partition by)) sel ...
- MySQL分组排序(取第一或最后)
MySQL分组排序(取第一或最后) 方法一:速度非常慢,跑了30分钟 SELECT custid, apply_date, rejectrule FROM ( SELECT *, IF ( , ) A ...
- mysql 分组排序前n + 长表转宽表
MySQL数据库优化的八种方式(经典必看) 建表 CREATE TABLE if not EXISTS `bb` ( `id` int not null primary key auto_increm ...
- mysql分组排序加序号(不用存储过程,就简简单单sql语句哦)
做前端好长时间了,好久没动sql了.在追一个喜欢的女孩,做测试的,有这么个需求求助与本屌丝,机会难得,开始折腾起来,配置mysql,建库,建表.... 一 建表 CREATE TABLE `my_te ...
- 关于oracle分组排序取值的问题
按照 某字段分组 某字段排序 然后取出该分组中排第1条数据(每组只取一条) SELECT* FROM( SELECT a.*,row_number() over(partition by ORI_FE ...
- Oracle 中分组排序取值的问题
整理一下排序: 建表语句:create table EXAM( name VARCHAR2(32), subject VARCHAR2(32), score INTEGER)数据:IN ...
- [转] 分组排序取前N条记录以及生成自动数字序列,类似group by后 limit
前言: 同事的业务场景是,按照cid.author分组,再按照id倒叙,取出前2条记录出来. oracle里面可以通过row_number() OVER (PARTITIO ...
- mySql分组排序
mysql 排序学习---mysql 1.建表语句 CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varc ...
- mysql 分组后取每个组内最新的一条数据
首先,将按条件查询并排序的结果查询出来. mysql order by accepttime desc; +---------------------+------+-----+ | acceptti ...
随机推荐
- bootloader研究最后一关(中)
2011-03-12 17:04:13 今天的目的是要把bin文件烧录到nandflash中. 其实我有一个问题一直没弄明白.S3C2440自带的RAM和ROM是多大?看了规格书说它能控制1G的8个b ...
- Centos 编译 安装 criu
下载依赖库 sudo yum install protobuf protobuf-c protobuf-c-devel protobuf-compiler protobuf-devel protobu ...
- nginx 反向代理和正向代理功能 第六章
一:Nginx作为正向代理服务器: 1.正向代理:代理(proxy)服务也可以称为是正向代理,指的是将服务器部署在公司的网关,代理公司内部员工上外网的请求,可以起到一定的安全作用和管理限制作用,正向代 ...
- index read-only
系统重启后,Eleastisearch6.5.0在给 Eleastisearch 更新索引的时候报了一个错误:ClusterBlockException[blocked by: [FORBIDDEN/ ...
- iOS UIAlertController在iPhone与iPad上的区别
很简单的一段代码: // 首先声明一个UIAlertController对象 private var alertController: UIAlertController! // 初始化UIAlert ...
- topcoder srm 580 div1
problem1 link 最优选择一定是在$2n$个端点中选出两个. problem2 link 分开考虑每个区间.设所有区间的左端点的最大值为$lc$,所有区间的右端点的最小值为$rc$.对于某个 ...
- linux驱动编写(pwm驱动)【转】
本文转载自:https://blog.csdn.net/feixiaoxing/article/details/79889240 pwm方波可以用来控制很多的设备,比如它可以被用来控制电机.简单来说, ...
- Arch Linux安装后的一些初始设置简介
配置有线网络. 没网络的时候,可以直接设定ip应急,后面 netctl 才是正规设置: # ip addr add 192.168.0.100/24 dev enp0s4# ip link set d ...
- Shell脚本中的分号使用
在Linux中,语句中的分号一般用作代码块标识 1.单行语句一般要用到分号来区分代码块,例如: if [ "$PS1" ]; then echo test is ok; fi te ...
- POJ2955--Brackets 区间DP入门 括号匹配
题意很简单,就是求给出串中最大的括号匹配数目.基础题,格式基本为简单区间dp模板. #include<iostream> #include<string.h> using na ...