将结果分组为多维数组

w3nuxt5m  于 2021-06-25  发布在  Mysql
关注(0)|答案(6)|浏览(432)

我需要得到数据分组 created_date 然后再次在数据集上摸索这个结果 affiliate_ad . 我在用这个

return DB::table($this->table)
      ->whereRaw($where['rawQuery'], isset($where['bindParams']) ? $where['bindParams'] : array())
      ->select('id', 'created_date','affiliate_ad', DB::raw('count(*) as total,count(affiliate_ad=1) as affiliate_ad_count,SUBSTRING(`created_date`, 1, 10) AS c_date'))
      ->groupBy('affiliate_ad','c_date')
      ->orderBy('c_date', 'desc')
      ->get();

它给了我这样的结果

Collection {#385
   #items: array:18 [
    0 => {#386
      +"id": 354766
      +"created_date": "2018-01-10 10:16:27"
      +"affiliate_ad": 1
      +"total": 2
      +"affiliate_ad_count": 1
      +"c_date": "2018-01-10"
    }
    1 => {#384
      +"id": 354730
      +"created_date": "2018-01-10 10:10:39"
      +"affiliate_ad": 0
      +"total": 3
      +"affiliate_ad_count": 4
      +"c_date": "2018-01-10"
    }
    2 => {#387
      +"id": 338263
      +"created_date": "2018-01-08 10:10:52"
      +"affiliate_ad": 0
      +"total": 83
      +"affiliate_ad_count": 83
      +"c_date": "2018-01-08"
    }
  ]
}

在这里,如果您检查,在前两个索引中,创建日期是相同的。所以我想把它们分组在一个数组索引中 0th 索引组件 multidimensional array 分组于 affiliate_ad . 实际查询生成为

SELECT id
     , created_date
     , affiliate_ad
     , COUNT(*) total
     , COUNT(affiliate_ad = 1) affiliate_ad_count
     , SUBSTRING(created_date,1,10) c_date 
  FROM facebook_ad 
 WHERE facebook_id = 12345 
   AND reward_status = 0 
   AND (first_seen BETWEEN 0 AND 99999999) 
 GROUP 
    BY affiliate_ad
     , c_date 
 ORDER 
    BY c_date desc

我需要这样的输出

Collection {#385
   #items: array:18 [
    0 => [
        0 => {#386
          +"id": 354766
          +"created_date": "2018-01-10 10:16:27"
          +"affiliate_ad": 1
          +"total": 2
          +"affiliate_ad_count": 1
          +"c_date": "2018-01-10"
       }
       1 => {#384
          +"id": 354730
          +"created_date": "2018-01-10 10:10:39"
          +"affiliate_ad": 0
          +"total": 3
          +"affiliate_ad_count": 4
          +"c_date": "2018-01-10"
        }
    ]
    1 => [
        0 => {#387
          +"id": 338263
          +"created_date": "2018-01-08 10:10:52"
          +"affiliate_ad": 0
          +"total": 83
          +"affiliate_ad_count": 83
          +"c_date": "2018-01-08"
        }
   ]
  ]
}

我在mysql中有这些数据

46qrfjad

46qrfjad1#

视为 id 是唯一的,并且您正在单独选择它,它不会在您的结果中分组。如果你需要个人身份证以及其他返回的数据,我建议使用 GROUP_CONCAT(id) .
更多信息请参见此处:https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-康卡特
不确定这是否完全回答了你的问题,但希望它能帮你指明正确的方向。

li9yvcax

li9yvcax2#

太长的评论,但很重要的事情要考虑…

SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+

SELECT COUNT(*) FROM ints;
+----------+
| COUNT(*) |
+----------+
|       10 |
+----------+

SELECT COUNT(i=1) FROM ints;
+------------+
| COUNT(i=1) |
+------------+
|         10 |
+------------+
wtzytmuj

wtzytmuj3#

return DB::table($this->table)
      ->whereRaw($where['rawQuery'], isset($where['bindParams']) ? $where['bindParams'] : array())
      ->select('id', 'created_date','affiliate_ad', DB::raw('count(*) as total,count(affiliate_ad=1) as affiliate_ad_count,SUBSTRING(`created_date`, 1, 10) AS c_date'))
      ->groupBy('affiliate_ad','c_date')
      ->orderBy('c_date', 'desc')
      ->get()->groupBy('affiliate_ad');

如果在config/database.php更改中出现错误,这将适用于您

strict => false,
siv3szwd

siv3szwd4#

与下图相似

您可以考虑在查询得到结果后对其进行格式化。它已经还给你一个收藏。你可以加上 ->groupBy('c_date')->values() 以检索给定结果。如果你移除 ->values() 它将保留分组日期作为键。

s8vozzvw

s8vozzvw5#

使用groupby的方法只适用于mysql中的聚合,比如 SUM 或者 MAX 等等。
虽然我还没有测试过,但您可以这样做,当您从数据库中获取结果时,请执行以下操作:

$results->groupBy('affiliate_ad');

foreach ($results as $i => $ad_group) {
    foreach ($ad_group as $items) {
        $results[$i] = (new Collection($items))->groupBy('c_date');  
    }
}

注意:取决于laravel的版本 DB 返回数组或 Collection 对象,如果它在您的版本中返回一个数组,则将其放入如下的集合对象中 new \Illuminate\Support\Collection($results)

0md85ypi

0md85ypi6#

改变

count(affiliate_ad=1)

SUM(affiliate_ad=1)

如果要“计数”行数 affiliate_ad1 .
也就是说, COUNT 忽略您在paren中输入的内容,除非它是一列。在这种情况下,它统计有多少行包含该列 NOT NULL . COUNT(DISTINCT col) 做了另一件事。 SUM(expression) 计算表达式并将值相加。如果 expression 是布尔值(例如, affiliate_ad=1 ),则true被视为1,false被视为0。因此,它会给出您可能想要的“计数”。
当心怎么做 NULLs 计算/求和/平均。

相关问题