0

Hello I'm about to explode.. I have been trying for hours now getting this to work. I have found different solutions for others and tried to rewrite it so it fits to my script but it just wont work as it should.

With reference from Barmar to this post: Join tables with SUM issue in MYSQL I have got a little way forward but I can't get it to print out what it should.

I have tried a lot of variant and moved around the JOINS etc. but without any luck. This is the query that gets nearest my wanted result:

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

You can see the outcome of this result in this picture (to the right):

whats_printing.jpg

The two first tables is pictures of the queries separately and is what it should print out. Just in one query.

I have tried with these two subqueries:

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

but then it's not printing anything. This works:

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

And also this (if I run them separately):

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

But how can I combine those two to one query?

Community
  • 1
  • 1
Kenneth
  • 31
  • 4
  • Just join two subqueries. – Paul Spiegel Jan 18 '17 at 23:10
  • You're misusing the notorious nonstandard MySQL extension to GROUP BY. A good first step to troubleshooting this query would be to get rid of the unpredictability in your results by using standard GROUP BY. Read this. https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html – O. Jones Jan 19 '17 at 01:26
  • Holy your question is really long. You can remove those text unnecessary and only keep those you'd like someone to help you out, will attractive more people to read and answer :D – SIDU Jan 19 '17 at 03:07
  • Thanks for your comment SIDU. I started with half the text/code but the reason that it became so long was because of the advise to try out subqueries etc. So I added this to the question. I will reduce it. :-) – Kenneth Jan 19 '17 at 14:09

0 Answers0