mysql分组获得最大的时间_通过最大(时间)mysql分组

bd96500e110b49cbb3cd949968f18be7.png

First of all this is kind of a duplicate to:

GROUP BY having MAX date

I am posting the question because the accepted answer doesn't work for me and I have no idea why. My problem:

I want to select the latest (max(timestamp)) checksum of all functions (func_ids).

The code from @Bill Karwin (accepted answer)

SELECT func_id,checksum

FROM Content cnt

INNER JOIN (

SELECT func_id, MAX(timestamp) AS maxdate

FROM Content GROUP BY func_id

) AS max USING (func_id,maxdate);

Mysql error:

#1054 - Unknown column 'maxdate' in 'from clause'

My table:

CREATE TABLE `Content` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`func_id` int(6) NOT NULL,

`description` text CHARACTER SET utf8 NOT NULL,

`returns` varchar(255) CHARACTER SET utf8 NOT NULL,

`var` varchar(255) CHARACTER SET utf8 NOT NULL,

`content` text CHARACTER SET utf8 NOT NULL,

`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

`checksum` varchar(40) CHARACTER SET utf8 DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `func_id` (`func_id`),

KEY `var` (`var`),

KEY `checksum` (`checksum`),

FULLTEXT KEY `description` (`description`)

) ENGINE=MyISAM AUTO_INCREMENT=885 DEFAULT CHARSET=latin1

解决方案

As I understand the sintaxis from MySQL when you put USING for an Inner Join the columns need to be named the same in both tables. There is no column named maxdate on the content table so the error jumps. You may be able to try (if I understand things correctly)

SELECT func_id,checksum

FROM Content cnt

INNER JOIN (

SELECT func_id, MAX(timestamp) AS maxdate

FROM Content GROUP BY func_id

) AS max ON (cnt.func_id=max.func_id AND max.maxdate=cnt.timestamp);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值