mysql分组取每组前几条记录

1、表SQL:

DROP TABLE IF EXISTS `ho_archives`;
CREATE TABLE `ho_archives` (
  `id` mediumint(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID',
  `type` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '所属栏目',
  `title` varchar(80) NOT NULL DEFAULT '' COMMENT '标题 ',
  `senddate` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '发布时间',
  `isdelete` tinyint(11) NOT NULL DEFAULT '0' COMMENT '逻辑删除状态0:不删除1:假删除',
  PRIMARY KEY (`id`),
  KEY `arctypeid` (`type`)
) ENGINE=MyISAM AUTO_INCREMENT=19 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='文档自定义属性表';

 

INSERT INTO `ho_archives` VALUES ('1', '16', '网站漏洞检测服务', '1379300153', '0');
INSERT INTO `ho_archives` VALUES ('3', '16', '网站安全监控', '1379300340', '0');
INSERT INTO `ho_archives` VALUES ('4', '16', '网站应急响应服务', '1379300358', '0');
INSERT INTO `ho_archives` VALUES ('5', '16', '网站代码加固', '1379300370', '0');
INSERT INTO `ho_archives` VALUES ('6', '14', '网站服务器安全加固', '1379300782', '0');
INSERT INTO `ho_archives` VALUES ('7', '14', '国内信息安全存5大盲区', '1379397987', '0');
INSERT INTO `ho_archives` VALUES ('8', '14', 'Gartner揭示十大IT安全误区', '1379398105', '0');
INSERT INTO `ho_archives` VALUES ('9', '14', '黑客组织“匿名者”威胁', '1383567878', '0');
INSERT INTO `ho_archives` VALUES ('10', '17', '网站安全服务项目-安全检测授权书', '1384131921', '0');
INSERT INTO `ho_archives` VALUES ('11', '17', '网站安全服务项目-安全检测授权书', '1384132192', '0');
INSERT INTO `ho_archives` VALUES ('12', '17', '如何利用Google机器人进行SQL攻击', '1384149836', '0');
INSERT INTO `ho_archives` VALUES ('13', '17', '网站安全服务项目-安全检测授权书', '1384161419', '0');
INSERT INTO `ho_archives` VALUES ('14', '13', '对于即将出台的等保标准', '1533605926', '0');
INSERT INTO `ho_archives` VALUES ('15', '13', '《网络安全法实施指南 、一》', '1533611060', '0');
INSERT INTO `ho_archives` VALUES ('16', '13', '《网络安全法实施指南、二》', '1533612538', '0');
INSERT INTO `ho_archives` VALUES ('17', '13', '《国家网络空间安全战略》全文', '1533623084', '0');
INSERT INTO `ho_archives` VALUES ('18', '13', '《国家网络安全事件应急预案》', '1533624631', '0');

 

2、查询sql:

select a.*
from ho_archives a 
where (select count(*) from ho_archives where type=a.type and a.id < id)<3
order by a.id desc;

3、结果:

mysql分组取每组前几条记录_第1张图片

你可能感兴趣的:(ThinkPHP,MySQL,PHP,笔记大全)