【发布时间】:2016-11-22 03:19:37
【问题描述】:
我已经检查了具有类似问题的帖子(1、2、3 ...),但在 MySQL57(mysql-installer-community-5.7.13.0.msi)上没有适合我的解决方案。我想要做的是反向“分组”(从表格底部向上)。真的不明白为什么下面的查询不起作用。
use db;
# create the table. k guarantees increase.
CREATE TABLE `db`.`test` (
`k` INT NOT NULL,
`a` INT NULL,
`b` INT NULL,
`c` INT NULL,
PRIMARY KEY (`k`));
# populate data
INSERT INTO `db`.`test` (`k`, `a`, `b`, `c`) VALUES ('1', '1', '10', '100');
INSERT INTO `db`.`test` (`k`, `a`, `b`, `c`) VALUES ('2', '2', '20', '200');
INSERT INTO `db`.`test` (`k`, `a`, `b`, `c`) VALUES ('3', '1', '10', '300');
INSERT INTO `db`.`test` (`k`, `a`, `b`, `c`) VALUES ('4', '3', '30', '700');
INSERT INTO `db`.`test` (`k`, `a`, `b`, `c`) VALUES ('5', '3', '30', '800');
# want to query the last entry of each unique a+b
Select * From
(Select * From test Order By k Desc) As t
Group By t.a, t.b;
我得到的是
k a b c
1 1 10 100
2 2 20 200
4 3 30 700
但我想要的是如下,顺序无所谓。
k a b c
5 3 30 800
3 1 10 300
2 2 20 200
【问题讨论】: