查询各分类中最大自增ID

时间:2021-06-08 23:28:49

假如现在有表结构:

CREATE TABLE `msg_detail` (
`id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
`title` VARCHAR(200) NOT NULL DEFAULT '' COMMENT '标题',
`content` TEXT NOT NULL COMMENT '内容',
`audio_url` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '音频地址',
`type_id` TINYINT(4) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
INDEX `cid` (`cid`) USING BTREE,
INDEX `column_id` (`column_id`) USING BTREE
)
COMMENT='内参表'
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=1;

  查询所有类型内参最大id,可用如下sql:

select max(id),type_id from msg_detail group by type_id;