使用子查询和多个JOINS显示SUM为两列的行

时间:2017-01-18 22:58:29

标签: mysql

你好,我即将爆炸..我已经尝试了几个小时,现在让它工作。我已经为其他人找到了不同的解决方案,并尝试重写它以使其适合我的脚本,但它不会正常工作。

Barmar参考了这篇文章:Join tables with SUM issue in MYSQL 我有一点前进的方向,但我无法打印它应该是什么。

我尝试了很多变种并在JOINS等地移动但没有任何运气。这是最接近我想要的结果的查询:

SELECT
    qs.statistic_ref_id,
    qs.question_id,
    SUM(qs.correct_count) AS count_correct,
    qs2.total_count_2 AS total_count,
    sr.user_id,
    sr.quiz_id,
    qq.title AS ny_titel,
    qq.category_id,
    SUBSTRING(qq.title,1,4) AS get_type_number,
    pl.points
    FROM pro_quiz_statistic AS qs 
    JOIN pro_quiz_statistic_ref sr ON sr.statistic_ref_id = qs.statistic_ref_id
    JOIN pro_quiz_question qq ON qq.id = qs.question_id
    JOIN user_points_log pl ON pl.quiz_id = sr.quiz_id AND pl.user_id = '$user_id'

    JOIN ( SELECT
        qs3.statistic_ref_id,
        qs3.question_id,
        SUM(qs3.correct_count + qs3.incorrect_count) AS total_count_2,
        sr3.user_id,
        sr3.quiz_id,
        qq3.title AS ny_titel_2,
        qq3.category_id,
        SUBSTRING(qq3.title,1,4) AS get_type_number_2,
        pl3.points
        FROM pro_quiz_statistic AS qs3 
        JOIN pro_quiz_statistic_ref sr3 ON sr3.statistic_ref_id = qs3.statistic_ref_id
        JOIN pro_quiz_question qq3 ON qq3.id = qs3.question_id
        JOIN user_points_log pl3 ON pl3.quiz_id = sr3.quiz_id AND pl3.user_id = '$user_id'
        WHERE sr3.user_id = '$user_id' AND
        qq3.category_id = '3'
        GROUP BY get_type_number_2 ORDER BY qs3.question_id 
    ) qs2 ON qs2.statistic_ref_id = sr.statistic_ref_id
    WHERE sr.user_id = '$user_id' AND
    qq.category_id = '3'
    GROUP BY get_type_number ORDER BY qs.question_id

您可以在此图片(右侧)中看到此结果的结果:

whats_printing.jpg

两个第一个表是分开的查询图片,它应该打印出来。只是在一个查询中。

我尝试过这两个子查询:

SELECT

    qs.statistic_ref_id,
    qs.question_id,
    qs2.correct_count AS count_correct,
    qs3.total_count_2 AS total_count,
    sr.user_id,
    sr.quiz_id,
    qq.category_id,
    SUBSTRING(qq.title,1,4) AS get_type_number,
    pl.points
    FROM pro_quiz_statistic AS qs
    JOIN pro_quiz_statistic_ref sr ON sr.statistic_ref_id = qs.statistic_ref_id
    JOIN pro_quiz_question qq ON qq.id = qs.question_id
    JOIN user_points_log pl ON pl.quiz_id = sr.quiz_id AND pl.user_id = '$user_id'
    JOIN (

    SELECT
        qs2.statistic_ref_id,
        qs2.question_id,
        SUM(qs2.correct_count) AS count_correct_2,
        sr2.user_id,
        sr2.quiz_id,
        qq2.category_id,
        SUBSTRING(qq2.title,1,4) AS get_type_number_2,
        pl2.points
        FROM pro_quiz_statistic AS qs2 
        JOIN pro_quiz_statistic_ref sr2 ON sr2.statistic_ref_id = qs2.statistic_ref_id
        JOIN pro_quiz_question qq2 ON qq2.id = qs2.question_id
        JOIN user_points_log pl2 ON pl2.quiz_id = sr2.quiz_id AND pl2.user_id = '$user_id'
        WHERE sr2.user_id = '$user_id' AND
        qq2.category_id = '3'
        GROUP BY get_type_number_2 
        ORDER BY qs2.question_id
    ) qs2 ON qs2.statistic_ref_id = qs.statistic_ref_id
    JOIN ( SELECT
        qs3.statistic_ref_id,
        qs3.question_id,
        SUM(qs3.correct_count + qs3.incorrect_count) AS total_count_2,
        sr3.user_id,
        sr3.quiz_id,
        qq3.category_id,
        SUBSTRING(qq3.title,1,4) AS get_type_number_3,
        pl3.points
        FROM pro_quiz_statistic AS qs3 
        JOIN pro_quiz_statistic_ref sr3 ON sr3.statistic_ref_id = qs3.statistic_ref_id
        JOIN pro_quiz_question qq3 ON qq3.id = qs3.question_id
        JOIN user_points_log pl3 ON pl3.quiz_id = sr3.quiz_id AND pl3.user_id = '$user_id'
        WHERE sr3.user_id = '$user_id' AND
        qq3.category_id = '3'
        GROUP BY get_type_number_3 
        ORDER BY qs3.question_id 
    ) qs3 ON qs3.statistic_ref_id = qs.statistic_ref_id

    WHERE sr.user_id = '$user_id' AND
    qq.category_id = '3'
    GROUP BY get_type_number 
    ORDER BY qs.question_id

然后它不打印任何东西。这有效:

SELECT
    qs.statistic_ref_id,
    qs.question_id,
    SUM(qs.correct_count) AS count_correct,
    sr.user_id,
    sr.quiz_id,
    qq.title AS ny_titel,
    qq.category_id,
    SUBSTRING(qq.title,1,4) AS get_type_number,
    pl.points
    FROM pro_quiz_statistic AS qs 
    JOIN pro_quiz_statistic_ref sr ON sr.statistic_ref_id = qs.statistic_ref_id
    JOIN pro_quiz_question qq ON qq.id = qs.question_id
    JOIN user_points_log pl ON pl.quiz_id = sr.quiz_id AND pl.user_id = '$user_id'

    WHERE sr.user_id = '$user_id' AND
    qq.category_id = '3'
    GROUP BY get_type_number ORDER BY qs.question_id

还有这个(如果我单独运行它们):

SELECT
    qs.statistic_ref_id,
    qs.question_id,
    SUM(qs.correct_count + qs.incorrect_count) AS count_correct,
    sr.user_id,
    sr.quiz_id,
    qq.title AS ny_titel,
    qq.category_id,
    SUBSTRING(qq.title,1,4) AS get_type_number,
    pl.points
    FROM pro_quiz_statistic AS qs 
    JOIN pro_quiz_statistic_ref sr ON sr.statistic_ref_id = qs.statistic_ref_id
    JOIN pro_quiz_question qq ON qq.id = qs.question_id
    JOIN user_points_log pl ON pl.quiz_id = sr.quiz_id AND pl.user_id = '$user_id'
    WHERE sr.user_id = '$user_id' AND
    qq.category_id = '3'
    GROUP BY get_type_number ORDER BY qs.question_id

但是如何将这两个查询结合起来呢?

0 个答案:

没有答案