【问题标题】:MySQL Inner Query with a Where and Group by conditions in cakephpMySQL 内部查询在 cakephp 中具有 Where 和 Group by 条件
【发布时间】:2009-10-12 09:22:12
【问题描述】:

我有一个名为 Reports 的表格

 id report_id user_id 
  1     1       5
  2     1       5
  3     1       5
  4     2       5
  5     2       5
  6     3       6
  7     3       6
  8     4       1
  9     4       1
 10     4       1     

我正在尝试编写一个查询,使得 user_id = 5 并查找他创建了多少个报告。(答案应该是 2)

我写了一个 Mysql 查询

  select count(distinct report_id) from Reports where user_id=5

我正在 Foreach 用户循环中尝试相同的 MYSQl 子查询,其中我的 5 来自 $user['User']['id'];

如何在 cakephp 框架的这个 for 循环中编写上面的 MYSQL 查询......

          foreach($users as & $user):

                echo "User id ".$user['User']['id'];

            $user['User']['report_count'] = $this->Report->find('count',
            array('conditions'=>array('Report.user_id'=>$user['User']['id'])));


        endforeach;

         $this->set('users', $users);

请给我建议.......如何在 cakephp 中编写上面的 Mysql 查询

【问题讨论】:

  • 您使用什么框架来提供数据访问?换句话说,是什么提供了find() 方法?

标签: mysql cakephp


【解决方案1】:

您想使用以下函数GROUP BYCOUNT

您的查询可能看起来像这样

select count(distinct report_id) from Reports where user_id=5

【讨论】:

  • 该查询将返回给定报告的 ID 数。我相信你的意思是select COUNT(DISTINCT report_id) from Reports where user_id=5
【解决方案2】:

如果这是您在应用程序中显示的用户列表...您可以显着减少正在运行的查询数量。 例如。对于 100 个用户,您将运行 100 个查询,而不是您可以运行单个查询来提取 user_id 和每个用户的报告计数

select count(distinct report_id) as count,user_id from Reports where user_id IN (1,2) GROUP BY user_id;

或者,如果您想为每个用户运行单独的查询

select count(distinct report_id) as count,user_id from Report where user_id=5;

【讨论】:

    【解决方案3】:

    试试这个:

    $user['User']['report_count'] = $this->Report->find('count',
        array( 'conditions' => array('Report.user_id' => $user['User']['id']),
            'fields' => 'DISTINCT Report.report_id'
        )
    );
    

    它应该为给定的user_id 获取所有不同的report_ids,然后计算它们。基本上,它应该运行查询:

    SELECT DISTINCT report_id FROM Reports WHERE user_id=$user['User']['id']
    

    (代入$user['User']['id']的值后),然后统计结果中的行数。警告:我在现实生活中不使用 CakePHP,我只是阅读了documentation;你的旅费可能会改变。正如 halocursed 所提到的,自己运行单个 SQL query 比为每个用户 ID 调用 find(...) 更有效。你也可以试试:

    $report_counts = $this->Report->find('list',
        array( 'conditions' => array('Report.user_id' => array_map(create_function('$user', 'return $user["User"]["id"];'), $users)),
            'group'  => array('Report.user_id'),
            'fields' => array('Report.user_id', 'COUNT(DISTINCT Report.report_id) AS report_count')
        )
    );
    foreach ($users as &$user) {
        $user['User']['report_count'] = $report_counts[$user['User']['id']];
    }
    

    但是,我不知道 CakePHP 是否会接受 'fields' 参数中的聚合函数,我也不知道 find('list', ...) 是否会选择 Report.user_id 作为数组索引。如果您在使用后者时遇到问题,您可以切换到[find('all', ...)][3] 调用并遍历$report_counts 而不是$users。我没有采用这种方法,因为我不知道 $users 的结构,比如它是如何被索引的。

    【讨论】:

      猜你喜欢
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      相关资源
      最近更新 更多