php - 复杂查询中的计数

标签 php mysql laravel-4

我将用一个例子来解释我的问题:

表格: 地点 - 评论 - activities_comment - 事件

用户可以评论某个地点并选择在该地点可以进行哪些事件。 事件例如:运行、游泳、步行和攀爬。

所以一些用户会投票超过1个事件,所以我不能把它放在同一个表COMMENT中,我需要创建一个数据透视表ACTIVITIES_COMMENT,问题就在这里,我是能够显示所有评论以及在每个地方选择的事件..

但现在我想计算事件的数量,并按照每个地点的评论中用户选择最多的事件到选择较少的事件对它们进行排序。 我怎样才能做到这一点??

我能做的最好的事情是:

$place = Place::with('city','comments')
            ->where('id',$placeId)->first();


$place->activities = Activity::join('activities_comment', 'activity.id', '=', 'activities_comment.activity_id')
            ->join('comment', 'comment.id', '=', 'activities_comment.comment_id')
            ->select('comment.id','activities_comment.activity_id',
                DB::raw('(SELECT count(activities_comment.activity_id) FROM activities_comment WHERE activities_comment.activity_id = 1) as run'),
                DB::raw('(SELECT count(activities_comment.activity_id) FROM activities_comment WHERE activities_comment.activity_id = 2) as swim'),
                DB::raw('(SELECT count(activities_comment.activity_id) FROM activities_comment WHERE activities_comment.activity_id = 3) as walk'),
                DB::raw('(SELECT count(activities_comment.activity_id) FROM activities_comment WHERE activities_comment.activity_id = 4) as climb'),
                )
            ->where('comment.place_id',$place->id)
            ->get();

问题是这个查询计算了最多选择的事件,但在所有地方,我只想在每个地方进行计数。

<小时/>

编辑:

示例行:

地点表:

id | name
----------
1  | Alaska
2  | Peru
3  | Argentina

评论表:

id | user_id | place_id | text
------------------------------------
1  | 1       | 1        | some text
2  | 3       | 1        | some text
3  | 2       | 2        | some text

事件表:

id | name 
----------
1  | run
2  | swim
3  | walk
4  | climb

activity_comment表:

id | comment_id | activity_id
------------------------------
1  | 1          | 1        
2  | 1          | 2        
3  | 2          | 2       
4  | 3          | 2

当我进入阿拉斯加评论时,我想查看用户在那里选择事件的次数,对于阿拉斯加,它将显示:

run: 1 time
swim: 2 times
walk: 0 times
climb: 0 times

如果我去秘鲁评论:

run: 0 times
swim: 1 time
walk: 0 times
climb: 0 times

提前谢谢您。

最佳答案

首先,通过连接将所有表链接起来,显示每个表如何通过各自的键与下一个表相关。然后,只需每个名称(城市/位置)、事件名称以及获取按相应位置和事件分组的计数即可。 order by 子句将把所有相同的位置放在第一位,然后在每个位置中将最高计数的事件放在次要位置...如果有相同的计数,则名称将按字母顺序排列

SELECT
      p.name,
      a.name as Activity,
      COUNT(*) as NumComments
   from
      place p
         join comment c
            ON p.id = c.place_id
            join activity_comment ac
               ON c.id = ac.comment_id
               join activity a
                  ON ac.activity_id = a.id
   group by
      p.name,
      a.name
   order by
      p.name,
      COUNT(*) desc,
      a.name

现在,您只需为您可能感兴趣的任何位置或事件插入 WHERE 子句(在分组依据之前)。

关于php - 复杂查询中的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21557758/

相关文章:

php - 数组插入mysql行

mysql - 删除检查命令在 MYSQL 中不起作用

mysql - Rails 多级关联链中不同模型的查询

php - 如何在html5上插入php

php - 使用 array_filter 时意外的 php 通知和警告

javascript - JQuery/javascript doubleclick=在 foreach() php 的每个 div 上执行一些操作

mysql - MySQL判断主键是否存在的方法

php - Laravel 身份验证问题

php - Laravel:使用自定义请求 header 测试路由

php - 使模型在 Laravel 中全局可用于布局