登录
首页 >  数据库 >  MySQL

得到每个分组里的最大最小记录 Mongo VS Mysql

来源:SegmentFault

时间:2023-02-17 15:55:20 483浏览 收藏

积累知识,胜过积蓄金银!毕竟在##column_title##开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《得到每个分组里的最大最小记录 Mongo VS Mysql》,就带大家讲解一下MySQL、mongodb知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

统计每个分组里的最大最小记录

mongo实现

{
     "_id" : "01001",
     "city" : "AGAWAM",
     "pop" : 15338,
     "state" : "MA"
}

完整json见: http://media.mongodb.org/zips...

需求

得到每个state拥有最多人口的城市和拥有最小人口的城市以及对应的人口数

db.zipcodes.aggregate(
    {$group: {_id:{state:"$state",city:"$city"}, popPerCity:{$sum:"$pop"} } },
    {$sort: {popPerCity:1} },
    {$group: {
        _id:"$_id.state",
        biggestCity:{$last:"$_id.city"},
        biggestPop: {$last:"$popPerCity"},
        smallestCity: {$first:"$_id.city"},
        smallestPop: {$first:"$popPerCity"}
    }}
)

效果

{ "_id" : "DE", "biggestCity" : "NEWARK", "biggestPop" : 111674, "smallestCity" : "BETHEL", "smallestPop" : 108 }
{ "_id" : "MS", "biggestCity" : "JACKSON", "biggestPop" : 204788, "smallestCity" : "CHUNKY", "smallestPop" : 79 }
...

见: https://docs.mongodb.com/manu...

Mysql实现

相比mongo的直观 就要绕很多了

方案一

# 需要设置一个较大值 默认的1024还不够用
SET SESSION group_concat_max_len = 20480;

select state, substring_index(group_concat(city order by pop ),",",1) smallestCity, min(pop),substring_index(group_concat(city order by pop ),",",-1) biggestCity,  max(pop) from  (select state, city, sum(pop) pop from zipcode group by state, city) a group by state ;

参考

https://dev.mysql.com/doc/ref...
https://dev.mysql.com/doc/ref...

方案二

# 每个state分组里面分别按pop升序、降序排序 人为分配一个序号 均取序号一就得到了该分组的起止记录

select b.state, b.city smallestCity, b.pop smallestPop, c.city biggestCity, c.pop biggestPop from
( select state,city,pop,@rank:=if(@current_state=state, @rank+1, 1) rank, @current_state:=state from
(select state, city, sum(pop) pop from zipcode group by state, city) a,
(select @current_state:=NULL, @rank:=NULL) vars
order by a.state,a.pop
) b ,
( select state,city,pop,@rank:=if(@current_state=state, @rank+1, 1) rank, @current_state:=state from
(select state, city, sum(pop) pop from zipcode group by state, city) a,
(select @current_state:=NULL, @rank:=NULL) vars
order by a.state,a.pop desc
) c
where b.state = c.state and b.rank = 1 and c.rank = 1

补充

建表语句

CREATE TABLE `zipcode` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `zipcode` varchar(10) NOT NULL,
  `city` varchar(30) NOT NULL,
  `pop` int(11) NOT NULL,
  `state` varchar(5) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `zipcode` (`zipcode`),
  KEY `idx_state_city` (`state`,`city`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

json ==> batch insert sql

jq -c '[._id, .city, .pop, .state]' zips.json | sed 's/\[\(.*\)\]$/\1/' | awk -F, '{print "insert into zipcode select null," $1"," $2","$3","$4";"}' 

今天关于《得到每个分组里的最大最小记录 Mongo VS Mysql》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于mysql的内容请关注golang学习网公众号!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>