has JOIN和GROUP on hasMany关联

时间:2013-03-03 23:01:26

标签: mysql sql

我有3个模型(表格):

Presentation hasMany PresentationView hasMany SlideView

字段:

演示文稿:id, title

PresentationView:id, presentation_id

SlideView:id, presentation_view_id, duration

我需要一个查询来获取每个演示文稿的统计信息。统计数据是:

  1. 每个演示文稿的PresentationView数量
  2. 属于Presentation(通过PresentationView)的幻灯片视图中所有SlideView.duration的总持续时间
  3. 所以基本上它似乎是双JOIN和双GROUP但连接对我不起作用 - 我尝试了LEFT / INNER / RIGHT双连接的每个组合,我无法使其工作。我得到的最好的是演示文稿已将PresentationView分组,但持续时间只是来自SlideViews的SUMed,仅属于一个PresentationViews而不是所有的演示文稿......

    如果可能,我想避免嵌套的SELECT。只是JOIN / GROUP

2 个答案:

答案 0 :(得分:1)

首先是一个简单的JOIN和COUNT:

SELECT p.id, COUNT(*)
FROM Presentation p
JOIN PresentationView v ON p.id = v.presentation_id
GROUP BY p.id

第二个必须使用SUM(和JOIN):

SELECT p.id, SUM(s.duration)
FROM Presentation p
JOIN PresentationView v ON p.id = v.presentation_id
JOIN SlideView s ON v.id = s.presentation_view_id
GROUP BY p.id

如果您想在一个查询中同时使用它们:

SELECT p.id, SUM(s.duration), COUNT(DISTINCT v.id)
FROM Presentation p
JOIN PresentationView v ON p.id = v.presentation_id
JOIN SlideView s ON v.id = s.presentation_view_id
GROUP BY p.id

DISTINCT

的原因

表:

Presentation:    PresentationView:          SlideView:
p.id | title     v.id | presentation_id     s.id | presentation_view_id | duration
1    | abc       1    | 1                   1    | 1                    | 100
2    | xyz       2    | 1                   2    | 1                    | 150
                 3    | 1                   3    | 2                    | 200
                 4    | 1                   4    | 2                    | 250
                 5    | 1                   5    | 3                    | 300
                 6    | 2                   6    | 3                    | 400
                 7    | 2                   7    | 4                    | 500
                                            8    | 5                    | 600
                                            9    | 6                    | 100
                                            10   | 6                    | 200
                                            11   | 7                    | 350

组前的示例结果集:

p.id | v.id | s.id | s.duration
-------------------------------
1    | 1    | 1    | 100
1    | 1    | 2    | 150
1    | 2    | 3    | 200
1    | 2    | 4    | 250
1    | 3    | 5    | 300
1    | 3    | 6    | 400
1    | 4    | 7    | 500
1    | 5    | 8    | 600
2    | 6    | 9    | 100
2    | 6    | 10   | 200
2    | 7    | 11   | 350

没有明确的小组之后:

p.id | SUM | COUNT
------------------
1    | 8   | 2500
2    | 3   | 650

有明显的:

p.id | SUM | COUNT
------------------
1    | 5   | 2500
2    | 2   | 650

答案 1 :(得分:0)

同时我找到答案:

SELECT presentations.title, SUM(slide_views.duration), COUNT(DISTINCT presentation_views.id) 
FROM presentations 
LEFT JOIN presentation_views ON presentations.id = presentation_views.presentation_id 
LEFT JOIN slide_views ON presentation_views.id = slide_views.presentation_view_id 
GROUP BY presentations.id