mysql 根据类型分组 获取每个类型下的一条或者多条数据
数据表结构
CREATE TABLE `user_msg` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`msg` text,
`status` tinyint(2) DEFAULT '0' COMMENT '状态',
`addtime` int(11) DEFAULT NULL,
`type` tinyint(2) DEFAULT '0' COMMENT '消息类型 1 账户金额变动 2服务通知 3优惠促销',
`url` varchar(100) DEFAULT NULL COMMENT '跳转链接',
`img` varchar(150) DEFAULT NULL COMMENT '图片',
`order_id` int(11) DEFAULT '0' COMMENT '订单',
`price` decimal(10,2) DEFAULT '0.00' COMMENT '变动金额',
`msg_last_time` int(11) DEFAULT '0',
PRIMARY KEY (`id`),
KEY `index_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 COMMENT='用户消息';
数据

获取用户1 各类型数据一条
select * from (
select * from user_msg where user_id = 1 group by type, id desc
) as base
group by type
网友评论