我已创建:http://sqlfiddle.com/#!2/7bb44/1

CREATE TABLE if not exists tblA
(
id int(11) NOT NULL auto_increment ,
userid int(255),
 category int(255),
  unixtime int(255),
 PRIMARY KEY (id)
);


INSERT INTO tblA (userid,category,unixtime) VALUES
('1', '1','1438689946'),
('1', '2','1438690005'),
('1', '3','1438690007'),
('5', '1','1438690009'),
('2', '1','1438690005'),
('2', '1','1438690398'),
('1', '2','1438691020'),
('1', '3','1438691028'),
('4', '2','1438690005'),
('2', '3','1438691025'),
('2', '2','1438691020'),
('3', '3','1438691022');


Select * from tblA  group by category order by unixtime  desc;

但我得到了错误的值。这些值不包含正确的unixtime desc。我如何才能使其工作?我真的很感谢你的帮助。

最佳答案

请尝试此查询。如果两个unixtime是相同的,那么应该只显示1

Select a.*
from tblA a join
     (select category, max(unixtime) as maxut
      from tblA
      group by category
     ) c
     on a.category = c.category and a.unixtime = c.maxut
group by unixtime order by a.unixtime desc;

关于mysql - 无法按类别和unixtime desc分组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31809784/

10-11 08:23