美文网首页
mysql 根据类型分组

mysql 根据类型分组

作者: jtw | 来源:发表于2020-04-18 11:34 被阅读0次

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='用户消息';

数据


image.png

获取用户1 各类型数据一条

select * from (
    select * from user_msg where user_id = 1 group by type, id desc
    ) as base
group by type

相关文章

网友评论

      本文标题:mysql 根据类型分组

      本文链接:https://www.haomeiwen.com/subject/yjhvvhtx.html