将结果分组为多维数组

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

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

  1. return DB::table($this->table)
  2. ->whereRaw($where['rawQuery'], isset($where['bindParams']) ? $where['bindParams'] : array())
  3. ->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'))
  4. ->groupBy('affiliate_ad','c_date')
  5. ->orderBy('c_date', 'desc')
  6. ->get();

它给了我这样的结果

  1. Collection {#385
  2. #items: array:18 [
  3. 0 => {#386
  4. +"id": 354766
  5. +"created_date": "2018-01-10 10:16:27"
  6. +"affiliate_ad": 1
  7. +"total": 2
  8. +"affiliate_ad_count": 1
  9. +"c_date": "2018-01-10"
  10. }
  11. 1 => {#384
  12. +"id": 354730
  13. +"created_date": "2018-01-10 10:10:39"
  14. +"affiliate_ad": 0
  15. +"total": 3
  16. +"affiliate_ad_count": 4
  17. +"c_date": "2018-01-10"
  18. }
  19. 2 => {#387
  20. +"id": 338263
  21. +"created_date": "2018-01-08 10:10:52"
  22. +"affiliate_ad": 0
  23. +"total": 83
  24. +"affiliate_ad_count": 83
  25. +"c_date": "2018-01-08"
  26. }
  27. ]
  28. }

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

  1. SELECT id
  2. , created_date
  3. , affiliate_ad
  4. , COUNT(*) total
  5. , COUNT(affiliate_ad = 1) affiliate_ad_count
  6. , SUBSTRING(created_date,1,10) c_date
  7. FROM facebook_ad
  8. WHERE facebook_id = 12345
  9. AND reward_status = 0
  10. AND (first_seen BETWEEN 0 AND 99999999)
  11. GROUP
  12. BY affiliate_ad
  13. , c_date
  14. ORDER
  15. BY c_date desc

我需要这样的输出

  1. Collection {#385
  2. #items: array:18 [
  3. 0 => [
  4. 0 => {#386
  5. +"id": 354766
  6. +"created_date": "2018-01-10 10:16:27"
  7. +"affiliate_ad": 1
  8. +"total": 2
  9. +"affiliate_ad_count": 1
  10. +"c_date": "2018-01-10"
  11. }
  12. 1 => {#384
  13. +"id": 354730
  14. +"created_date": "2018-01-10 10:10:39"
  15. +"affiliate_ad": 0
  16. +"total": 3
  17. +"affiliate_ad_count": 4
  18. +"c_date": "2018-01-10"
  19. }
  20. ]
  21. 1 => [
  22. 0 => {#387
  23. +"id": 338263
  24. +"created_date": "2018-01-08 10:10:52"
  25. +"affiliate_ad": 0
  26. +"total": 83
  27. +"affiliate_ad_count": 83
  28. +"c_date": "2018-01-08"
  29. }
  30. ]
  31. ]
  32. }

我在mysql中有这些数据

46qrfjad

46qrfjad1#

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

li9yvcax

li9yvcax2#

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

  1. SELECT * FROM ints;
  2. +---+
  3. | i |
  4. +---+
  5. | 0 |
  6. | 1 |
  7. | 2 |
  8. | 3 |
  9. | 4 |
  10. | 5 |
  11. | 6 |
  12. | 7 |
  13. | 8 |
  14. | 9 |
  15. +---+
  16. SELECT COUNT(*) FROM ints;
  17. +----------+
  18. | COUNT(*) |
  19. +----------+
  20. | 10 |
  21. +----------+
  22. SELECT COUNT(i=1) FROM ints;
  23. +------------+
  24. | COUNT(i=1) |
  25. +------------+
  26. | 10 |
  27. +------------+
展开查看全部
wtzytmuj

wtzytmuj3#

  1. return DB::table($this->table)
  2. ->whereRaw($where['rawQuery'], isset($where['bindParams']) ? $where['bindParams'] : array())
  3. ->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'))
  4. ->groupBy('affiliate_ad','c_date')
  5. ->orderBy('c_date', 'desc')
  6. ->get()->groupBy('affiliate_ad');

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

  1. strict => false,
siv3szwd

siv3szwd4#

与下图相似

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

s8vozzvw

s8vozzvw5#

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

  1. $results->groupBy('affiliate_ad');
  2. foreach ($results as $i => $ad_group) {
  3. foreach ($ad_group as $items) {
  4. $results[$i] = (new Collection($items))->groupBy('c_date');
  5. }
  6. }

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

0md85ypi

0md85ypi6#

改变

  1. count(affiliate_ad=1)

  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 计算/求和/平均。

相关问题