首先,这是一个重复: GROUP BY having MAX date 我发帖是因为接受的答案对我不起作用,我不知道为什么。我的问题:
我想选择所有函数的最新(max(timestamp)
)校验和(func_ids
)。
来自@Bill Karwin的代码(接受回答)
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错误:
#1054 - Unknown column 'maxdate' in 'from clause'
我的表:
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
答案 0 :(得分:4)
正如我理解来自MySQL的sintaxis,当你将USING用于内连接时,两个表中的列需要被命名为相同。内容表上没有名为maxdate的列,因此错误会跳转。您可以尝试(如果我理解正确的话)
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);
答案 1 :(得分:3)
使用on
子句代替using
:
SELECT func_id,checksum
FROM Content cnt INNER JOIN
(SELECT func_id, MAX(timestamp) AS maxtimestamp
FROM Content
GROUP BY func_id
) m
on m.func_id = cnt.func_id and m.maxtimestamp = cnt.timestamp;
答案 2 :(得分:3)
当您加入... USING()时,USING的参数必须是出现在两个参与表中并完全匹配的列或列列表。您似乎没有内容
中的maxdate