Yii2框架mongo写法:
$data = TableModel::getCollection()->aggregate([
[
'$group'=>[
'_id'=>['register_number'=>'$register_number', 'type'=>'$type', 'sub_type'=>'sub_type'], //通过user_id分组去重
'total'=>[
'$sum'=> '$amount'
]
]
]
]);
mysql对应写法
SELECT sum(amount) as total,type,sub_type,register_number from table_name GROUP BY register_number,type,sub_type
以上两种等效
$data = Table::getCollection()->aggregate([
[
'$group' => [
'_id' => ['aid' => '$aid','uid' => '$uid'], //通过user_id分组去重
'total' => [
'$sum' => 1
]
],
],
[
'$match' => [
'total' => [
'$gt' => 1
]
]
]
],['allowDiskUse' => true]);
select aid,uid,count(1) as total from table group by aid,uid having count(1) > 1
参考链接:
https://blog.csdn.net/weixin_42648566/article/details/80970297
https://docs.mongodb.com/manual/reference/operator/aggregation/sum/