如何计算JOIN中的行数?

时间:2017-01-14 17:22:31

标签: mysql

我有四个表,我想与表" pro_quiz_statistic_ref"使用JOIN。除了一张桌子之外,所有桌子都加入了正常状态,并且#34; pro_quiz_stastic"。

表" pro_quiz_statistic_ref"正在举行用户参与的所有测验。

在pro_quiz_statistic中,每个测验都有10个答案的记录,当然还有statistic_ref_id来识别它所属的测验。 我想对列#34; correct_count"进行计数。在表格" pro_quiz_statistic"使用相同的statistic_ref_id。

我有这个问题:

$results_quiz = $pdo->query("SELECT 
    sr.statistic_ref_id, 
    sr.quiz_id, 
    sr.user_id, 
    sr.total_time, 
    qm.name AS q_name, 
    qm.category_id,
    qm.subcategory_id,
    sc.sub_category_id,
    sc.sub_category_name AS quiz_type,
    pl.time,
    pl.points,
    qs.correct_count AS count_correct 
    FROM pro_quiz_statistic_ref AS sr 
    JOIN pro_quiz_master qm ON qm.id = sr.quiz_id 
    JOIN pro_quiz_subcategory sc ON sc.sub_category_id = qm.subcategory_id
    JOIN user_points_log pl ON pl.quiz_id = sr.quiz_id AND pl.user_id = '$get_id'
    JOIN pro_quiz_statistic qs ON qs.statistic_ref_id = sr.statistic_ref_id
    WHERE sr.user_id = '$get_id' ORDER BY qm.name ASC");  

使用上面的代码,它打印出用户参与了" pro_quiz_statistic"

中列出的* 10个答案的所有测验

我试过写它像

COUNT(qs.correct_count) AS count_correct 

然后它只打印出一行。 BTW:它也被user_id过滤。

我想使用JOIN的原因是因为还有更多

有人可以帮我这个吗?

最好的问候

--------更新---------

我最终得到的结果很好:

$results_quiz = $pdo->query("SELECT 
    sr.statistic_ref_id, 
    sr.quiz_id, 
    sr.user_id, 
    sr.total_time, 
    qm.name AS q_name, 
    qm.category_id,
    qm.subcategory_id,
    sc.sub_category_id,
    sc.sub_category_name AS quiz_type,
    pl.time,
    pl.points,
    COUNT(qs.correct_count) AS count_correct 
    FROM pro_quiz_statistic_ref AS sr 
    JOIN pro_quiz_master qm ON qm.id = sr.quiz_id 
    JOIN pro_quiz_subcategory sc ON sc.sub_category_id = qm.subcategory_id
    JOIN user_points_log pl ON pl.quiz_id = sr.quiz_id AND pl.user_id = '$get_id'
    JOIN pro_quiz_statistic qs ON qs.statistic_ref_id = sr.statistic_ref_id
    WHERE 
    sr.user_id = '$get_id' AND 
    qs.correct_count = '1'
    GROUP BY     
    sr.statistic_ref_id,
    qs.correct_count
  ORDER BY qm.name ASC"); 

1 个答案:

答案 0 :(得分:0)

如果你需要像count(..)这样的聚合函数,你应该使用group by语句

  $results_quiz = $pdo->query("SELECT 
      sr.statistic_ref_id, 
      sr.quiz_id, 
      sr.user_id, 
      sr.total_time, 
      qm.name AS q_name, 
      qm.category_id,
      qm.subcategory_id,
      sc.sub_category_id,
      sc.sub_category_name AS quiz_type,
      pl.time,
      pl.points,
      count(qs.correct_count )AS count_correct 
      FROM pro_quiz_statistic_ref AS sr 
      JOIN pro_quiz_master qm ON qm.id = sr.quiz_id 
      JOIN pro_quiz_subcategory sc ON sc.sub_category_id = qm.subcategory_id
      JOIN user_points_log pl ON pl.quiz_id = sr.quiz_id AND pl.user_id = '$get_id'
      JOIN pro_quiz_statistic qs ON qs.statistic_ref_id = sr.statistic_ref_id
      WHERE sr.user_id = '$get_id'
      GROUP BY     
        sr.statistic_ref_id, 
        sr.quiz_id, 
        sr.user_id, 
        sr.total_time, 
        qm.name, 
        qm.category_id,
        qm.subcategory_id,
        sc.sub_category_id,
        sc.sub_category_name AS quiz_type,
        pl.time,
        pl.point
      ORDER BY qm.name ASC");