想要根据用户分组,以该用户的下单时间为降序,提取所有用户的第二个订单信息。
这属于分组排序,在Oracle有内置函数可以实现,而在mysql就有点麻烦:
CREATE TABLE user_orders (orders_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
add_time INT UNSIGNED NOT NULL,
PRIMARY KEY(orders_id),
KEY(user_id),
KEY(add_time)
)ENGINE=INNODB DEFAULT CHARSET utf8 COMMENT ‘mysql实现分组排序测试表‘;
INSERT INTO `user_orders` (`orders_id`, `user_id`, `add_time`) VALUES(‘1‘,‘1‘,‘1‘);
INSERT INTO `user_orders` (`orders_id`, `user_id`, `add_time`) VALUES(‘2‘,‘1‘,‘2‘);
INSERT INTO `user_orders` (`orders_id`, `user_id`, `add_time`) VALUES(‘3‘,‘1‘,‘3‘);
INSERT INTO `user_orders` (`orders_id`, `user_id`, `add_time`) VALUES(‘4‘,‘2‘,‘1‘);
INSERT INTO `user_orders` (`orders_id`, `user_id`, `add_time`) VALUES(‘5‘,‘2‘,‘2‘);
INSERT INTO `user_orders` (`orders_id`, `user_id`, `add_time`) VALUES(‘6‘,‘2‘,‘3‘);
INSERT INTO `user_orders` (`orders_id`, `user_id`, `add_time`) VALUES(‘7‘,‘3‘,‘1‘);
INSERT INTO `user_orders` (`orders_id`, `user_id`, `add_time`) VALUES(‘8‘,‘3‘,‘2‘);
INSERT INTO `user_orders` (`orders_id`, `user_id`, `add_time`) VALUES(‘9‘,‘3‘,‘3‘);
SELECT orders_id,user_id,add_time,rank FROM (
SELECT @rownum:=@rownum+1 AS rownum,# 行号
IF(@x=uo.user_id,@rank:=@rank+1,@rank:=1) rank,#处理排名,如果@x等于user_id,则表示@x被初始化,将@rank自增1
@x:=uo.user_id, # 初始化@x,@x为中间变量,在rank之后初始化,所以,rank初始化时,@x为null或者是上一个user_id的值
orders_id,user_id,add_time
FROM
user_orders uo,
(SELECT @rownum:=0,@rank:=0) init # 初始化信息表
ORDER BY user_id ASC, add_time DESC
)result
WHERE rank=2
重点:关键在于@x如何赋值。了解@x的赋值之后,立马就能明白rank(名次)的由来。
既然是分组排序,那当然是按组内来编号,每组当然得有一个不变的列,要不然按什么group by呢?抓住这个特点自然就理解了rank的含义,还有一个点要注意就是group by的时候要有两个排序的条件,要不然组内不稳定
------------------------------以上转自https://www.cnblogs.com/john8169/p/9780471.html
---------------------------------一下是我自己写的
SELECT x.role_id, CONCAT(x.role_name, ‘_‘, x.sort) AS new_role_name #将重复得名字后加上‘_编号’
FROM (
SELECT c.role_id, c.role_name, @sort := IF(c.role_name = @group, @sort + 1, 1) AS sort,@group := c.role_name #@group等于role_name时,@sort+1,否则返回1
FROM ( SELECT a.role_id, a.role_name, b.same_role_name_count
FROM temp_role a, (
SELECT role_name,COUNT(role_id) AS same_role_name_count #取到同一个account_id下role_name有重复
FROM temp_role
GROUP BY account_id,role_name
HAVING COUNT(role_id) > 1
) b
WHERE a.role_name = b.role_name
ORDER BY a.role_name,a.role_id
) c,
( SELECT @group := NULL, @sort := 1) temp ) x
原文地址:https://www.cnblogs.com/chengyunblogs/p/11596034.html