Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7992153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:29:11+00:00 2026-06-04T13:29:11+00:00

I have some tables liek this User : id , username answers : id

  • 0

I have some tables liek this

User         : id , username
answers      : id , answers , is_correct , question_id
user_answers : user_id , question_id , answer_id , date
questions    : id , question , category
category     : id name

Now what i am trying to do is count the correct answers user have given on each category. My query is this

SELECT
  u.`user_name` AS `UserName`,
  u.`user_id`   AS `UserId`,
  sum(a.is_correct) AS `Score`,
  group_concat(distinct c.name) AS `Quizes`
FROM users_answers ua
  LEFT JOIN answers a
    on a.id = ua.answer_id
  LEFT JOIN users u
    ON u.user_id = ua.user_id
  left join questions q
    on q.id = a.question_id
  left join categories c
    on c.id = q.cat_id
WHERE a.is_correct = 1
GROUP BY u.user_id
ORDER BY `Score` desc, ua.date desc;

It brings the result ok but i need to get the total score seperatly for each category.

EDITS

select
  s.UserName,
  s.UserId,
  GROUP_CONCAT(Score) AS CategoryScores,
  sum(s.TotalScore) as TotalScore,
  s.Quizes,
  s.LastPlayed
from (select
        u.`user_name` AS `UserName`,
        u.`user_id`   AS `UserId`,
        sum(a.is_correct) AS `Score`,
        sum(a.is_correct) AS `TotalScore`,
        group_concat(distinct c.name) AS `Quizes`,
        ua.date       as `LastPlayed`,
        c.id          as CategoryId
      FROM users_answers ua
        LEFT JOIN answers a
          on a.id = ua.answer_id
        LEFT JOIN users u
          ON u.user_id = ua.user_id
        left join questions q
          on q.id = a.question_id
        left join categories c
          on c.id = q.cat_id
      WHERE a.is_correct = 1
      GROUP BY u.user_id, CategoryId) s
GROUP BY UserId
ORDER BY `TotalScore` desc, LastPlayed desc;

Thanks Michael but the problem here is that now i cant get categories names in group concat.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:29:12+00:00Added an answer on June 4, 2026 at 1:29 pm

    Try This

    select
      UserName   as UserName,
      UserId,
      group_concat(Score) as Score,
      sum(TotalScore) as TotalScore,
      LastPlayed,
      group_concat(Quizes)    QuizNames
    from (SELECT
            u.`user_name` AS `UserName`,
            u.`user_id`   AS `UserId`,
            count(ua.`id`) AS `Score`,
            count(ua.`id`) AS `TotalScore`,
            ua.date       as `LastPlayed`,
            group_concat(distinct c.name) as `Quizes`
          FROM users_answers ua
            LEFT JOIN answers a
              on a.id = ua.answer_id
            LEFT JOIN users u
              ON u.user_id = ua.user_id
            left join questions q
              on q.id = a.question_id
            left join categories c
              on c.id = q.cat_id
          WHERE a.is_correct = 1
          GROUP BY u.user_id desc,c.id
          ORDER BY `Score` desc, ua.date desc,u.user_id) sub
    GROUP BY UserId desc
    ORDER BY `TotalScore` desc, LastPlayed desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some tables, they are: user ============================== user_id | username | etc.. ==============================
We have some large tables and are trying to optimize against. (Note: in this
I have some related tables and for the main this is the Model: <?php
I have some tables like this: USERS TABLE: | id | created | active
I have some tables like this : I. parent table : id_client id_group package
In my sqlite3 database I have some tables with user added values that I
I have some tables. Most important ones in this JOIN query are: table 'changes'
I'm getting sick with this problem. I have some tables that have contentes launched
So what I want is to have some tables like this <ul> <li>Some Text,
I have some tables which basically look as follows: TBL_USER user_id - number user_name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.