Mysql max not working, only max id getting but group by is not working
仅显示最小值
CREATE TABLE `feedbacklogs` (
`fid` int(11) NOT NULL AUTO_INCREMENT,
`remarks` varchar(255) NOT NULL,
`eid` int(11) NOT NULL,
PRIMARY KEY (`fid`)
) ENGINE=MyISAM AUTO_INCREMENT=4081 DEFAULT CHARSET=latin1
select status,max(fid) from feedbacklogs group by eid
我得到了错误的值
SELECT eid,remarks,max(fid) FROM feedbacklogs
group by eid
请给我解决方法
样本数据
fid - 01 , status - active, eid-70
fid - 02 , status - deleted, eid-72
fid - 03 , status - deleted, eid-70
实际结果:fid-03,状态-已删除,eid-70
i get:fid-03,status-active,eid-70
答案 0 :(得分:1)
当group by中的列与SELECT中没有聚合函数的列不匹配时,这些列的值是不可预测的
(这种情况在mysql版本<5.7 for mysql> = 5.7时发生,默认情况下会使用group by引发错误)
setValue()
您应该使用
SELECT eid,remarks,max(fid) FROM feedbacklogs
group by eid
或
SELECT eid,remarks,max(fid) FROM feedbacklogs
group by eid, remarks
或者如果您想对max_fid进行评论
SELECT eid,min(remarks),max(fid) FROM feedbacklogs
group by eid