MYSQL Group By有连接和聚合值

时间:2013-03-12 17:22:25

标签: php mysql sql database doctrine

这是我的学说(1.2)查询的第一种形式,在页面底部是返回的内容。

$q = Doctrine_Query::create()
            ->select('i.id, i.time_estimate, i.assigned_to, ts.time_spent')
            ->from('Model_Issue i')
            ->innerJoin('i.User_2 user')
            ->leftJoin('i.IssuesTimeSpent ts')
            ->where('i.project_id = ?',$project_id);

我正试图用它来做这件事:

$q = Doctrine_Query::create()
            ->select('i.id, sum(i.time_estimate) as time_estimate, i.assigned_to, sum(ts.time_spent) as time_spent')
            ->from('Model_Issue i')
            ->innerJoin('i.User_2 user')
            ->leftJoin('i.IssuesTimeSpent ts')
            ->where('i.project_id = ?',$project_id)
            ->groupBy('assigned_to');

我期望两个用户(assigned_to)的返回时间与所有与assigned_to id相关的问题所花费的时间总和,与time_estimate相同。

但这是归来的;

array
  0 => 
    array
      'id' => string '1' (length=1)
      'assigned_to' => string '1' (length=1)
      'time_estimate' => string '238' (length=3) <-  this is wrong
      'time_spent' => string '72' (length=2) <-  this is correct i believe
  1 => 
    array
      'id' => string '7' (length=1)
      'assigned_to' => string '2' (length=1)
      'time_estimate' => string '3' (length=1) <-  this is wrong
      'time_spent' => string '30' (length=2) <-  this is correct i believe

我是否正在尝试正确的事情,我想要的输出只能用groupby和聚合值来实现吗?或者我需要做一些特别的事情吗?

此致

N.B。 groupby / sums之前的完整数组

 array
      0 => 
        array
          'id' => string '1' (length=1)
          'time_estimate' => string '4' (length=1)
          'assigned_to' => string '1' (length=1)
          'IssuesTimeSpent' => 
            array
              0 => 
                array
                  'id' => string '1' (length=1)
                  'time_spent' => string '2' (length=1)
              1 => 
                array
                  'id' => string '40' (length=2)
                  'time_spent' => string '1' (length=1)
      1 => 
        array
          'id' => string '2' (length=1)
          'time_estimate' => string '6' (length=1)
          'assigned_to' => string '1' (length=1)
          'IssuesTimeSpent' => 
            array
              0 => 
                array
                  'id' => string '2' (length=1)
                  'time_spent' => string '4' (length=1)
              1 => 
                array
                  'id' => string '5' (length=1)
                  'time_spent' => string '3' (length=1)
              2 => 
                array
                  'id' => string '6' (length=1)
                  'time_spent' => string '3' (length=1)
              3 => 
                array
                  'id' => string '8' (length=1)
                  'time_spent' => string '1' (length=1)
              4 => 
                array
                  'id' => string '9' (length=1)
                  'time_spent' => string '1' (length=1)
              5 => 
                array
                  'id' => string '10' (length=2)
                  'time_spent' => string '1' (length=1)
              6 => 
                array
                  'id' => string '11' (length=2)
                  'time_spent' => string '1' (length=1)
              7 => 
                array
                  'id' => string '12' (length=2)
                  'time_spent' => string '2' (length=1)
              8 => 
                array
                  'id' => string '13' (length=2)
                  'time_spent' => string '1' (length=1)
              9 => 
                array
                  'id' => string '14' (length=2)
                  'time_spent' => string '1' (length=1)
              10 => 
                array
                  'id' => string '15' (length=2)
                  'time_spent' => string '1' (length=1)
              11 => 
                array
                  'id' => string '16' (length=2)
                  'time_spent' => string '1' (length=1)
              12 => 
                array
                  'id' => string '17' (length=2)
                  'time_spent' => string '1' (length=1)
              13 => 
                array
                  'id' => string '18' (length=2)
                  'time_spent' => string '1' (length=1)
              14 => 
                array
                  'id' => string '19' (length=2)
                  'time_spent' => string '1' (length=1)
              15 => 
                array
                  'id' => string '20' (length=2)
                  'time_spent' => string '1' (length=1)
              16 => 
                array
                  'id' => string '21' (length=2)
                  'time_spent' => string '1' (length=1)
              17 => 
                array
                  'id' => string '22' (length=2)
                  'time_spent' => string '1' (length=1)
              18 => 
                array
                  'id' => string '23' (length=2)
                  'time_spent' => string '1' (length=1)
              19 => 
                array
                  'id' => string '24' (length=2)
                  'time_spent' => string '1' (length=1)
              20 => 
                array
                  'id' => string '25' (length=2)
                  'time_spent' => string '1' (length=1)
              21 => 
                array
                  'id' => string '26' (length=2)
                  'time_spent' => string '1' (length=1)
              22 => 
                array
                  'id' => string '27' (length=2)
                  'time_spent' => string '1' (length=1)
              23 => 
                array
                  'id' => string '28' (length=2)
                  'time_spent' => string '1' (length=1)
              24 => 
                array
                  'id' => string '29' (length=2)
                  'time_spent' => string '1' (length=1)
              25 => 
                array
                  'id' => string '30' (length=2)
                  'time_spent' => string '1' (length=1)
              26 => 
                array
                  'id' => string '31' (length=2)
                  'time_spent' => string '1' (length=1)
              27 => 
                array
                  'id' => string '32' (length=2)
                  'time_spent' => string '1' (length=1)
              28 => 
                array
                  'id' => string '33' (length=2)
                  'time_spent' => string '1' (length=1)
              29 => 
                array
                  'id' => string '34' (length=2)
                  'time_spent' => string '1' (length=1)
              30 => 
                array
                  'id' => string '35' (length=2)
                  'time_spent' => string '1' (length=1)
              31 => 
                array
                  'id' => string '36' (length=2)
                  'time_spent' => string '1' (length=1)
              32 => 
                array
                  'id' => string '37' (length=2)
                  'time_spent' => string '1' (length=1)
              33 => 
                array
                  'id' => string '38' (length=2)
                  'time_spent' => string '1' (length=1)
              34 => 
                array
                  'id' => string '39' (length=2)
                  'time_spent' => string '3' (length=1)
              35 => 
                array
                  'id' => string '42' (length=2)
                  'time_spent' => string '1' (length=1)
              36 => 
                array
                  'id' => string '43' (length=2)
                  'time_spent' => string '10' (length=2)
      2 => 
        array
          'id' => string '3' (length=1)
          'time_estimate' => string '4' (length=1)
          'assigned_to' => string '1' (length=1)
          'IssuesTimeSpent' => 
            array
              0 => 
                array
                  'id' => string '3' (length=1)
                  'time_spent' => string '3' (length=1)
              1 => 
                array
                  'id' => string '41' (length=2)
                  'time_spent' => string '10' (length=2)
      3 => 
        array
          'id' => string '7' (length=1)
          'time_estimate' => string '3' (length=1)
          'assigned_to' => string '2' (length=1)
          'IssuesTimeSpent' => 
            array
              0 => 
                array
                  'id' => string '7' (length=1)
                  'time_spent' => string '30' (length=2)

1 个答案:

答案 0 :(得分:0)

好的,我相信我找到了解决方案。是否是我不知道的最好的..

$q = Doctrine_Query::create()
            ->select('i.id, assigned.full_name as full_name, count(i.id) as issue_count, sum(i.time_estimate) as time_estimate, 
                (SELECT sum(ts.time_spent) as t FROM Model_IssuesTimeSpent ts WHERE ts.user_id = i.assigned_to) as time_spent')
            ->from('Model_Issue i')
            ->leftJoin('i.User_2 assigned')
            ->where('i.project_id = ?',$project_id)
            ->groupBy('assigned_to');

为timepent添加子查询,而不是加入它会停止所有奇怪的数字。 它似乎直接加入它导致我正在设置的组中的重复行..