CREATE TABLE `new_schema`.`demo` ( `id` INT NOT NULL, `person` VARCHAR(45) NOT NULL, `score` VARCHAR(45) NOT NULL, PRIMARY KEY (`id`));
INSERT INTO `new_schema`.`demo` (`id`, `person`, `score`) VALUES ('1', 'bob', '50');
INSERT INTO `new_schema`.`demo` (`id`, `person`, `score`) VALUES ('2', 'jake', '60');
INSERT INTO `new_schema`.`demo` (`id`, `person`, `score`) VALUES ('3', 'bob', '100');
INSERT INTO `new_schema`.`demo` (`id`, `person`, `score`) VALUES ('6', 'jake', '100');
INSERT INTO `new_schema`.`demo` (`id`, `person`, `score`) VALUES ('8', 'li', '100');
/* 1 */
{
"_id" : ObjectId("58043fa8e9a7804c05031e17"),
"person" : "bob",
"sorce" : 50
}
/* 2 */
{
"_id" : ObjectId("58043fa8e9a7804c05031e18"),
"person" : "bob",
"sorce" : 100
}
/* 3 */
{
"_id" : ObjectId("58043fa8e9a7804c05031e19"),
"person" : "jake",
"sorce" : 60
}
/* 4 */
{
"_id" : ObjectId("58043fa8e9a7804c05031e1a"),
"person" : "jake",
"sorce" : 100
}
/* 5 */
{
"_id" : ObjectId("58043fa8e9a7804c05031e1b"),
"person" : "li",
"sorce" : 100
}
SELECT person, SUM(score), AVG(score), MIN(score), MAX(score), COUNT(*)
FROM demo
WHERE score > 0 AND person IN('bob','jake')
GROUP BY person;
db.demo.aggregate(
{
"$match":{
"$and":[
{"sorce":{"$gt":0}},
{"person":{"$in":["bob","jake"]}}
]
}
}
/* 1 */
{
"_id" : ObjectId("58043fa8e9a7804c05031e17"),
"person" : "bob",
"sorce" : 50
}
/* 2 */
{
"_id" : ObjectId("58043fa8e9a7804c05031e18"),
"person" : "bob",
"sorce" : 100
}
/* 3 */
{
"_id" : ObjectId("58043fa8e9a7804c05031e19"),
"person" : "jake",
"sorce" : 60
}
/* 4 */
{
"_id" : ObjectId("58043fa8e9a7804c05031e1a"),
"person" : "jake",
"sorce" : 100
}
db.demo.aggregate(
{
"$match":{
"$and":[
{"sorce":{"$gt":0}},
{"person":{"$in":["bob","jake"]}}
]
}
},
{
"$group":{"_id":"$person",
"sumSorce":{"$sum":"$sorce"},
"avgSorce":{"$avg":"$sorce"},
"lowsetSorce":{"$min":"$sorce"},
"highestSorce":{"$max":"$sorce"},
"count":{"$sum":1}}
}
)
/* 1 */
{
"_id" : "bob",
"sumSorce" : 150,
"avgSorce" : 75.0,
"lowsetSorce" : 50,
"highestSorce" : 100,
"count" : 2.0
}
/* 2 */
{
"_id" : "jake",
"sumSorce" : 160,
"avgSorce" : 80.0,
"lowsetSorce" : 60,
"highestSorce" : 100,
"count" : 2.0
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有