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 8471427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:47:35+00:00 2026-06-10T16:47:35+00:00

My database structure looks like this: https://docs.google.com/open?id=0B9ExyO6ktYcOenZ1WlBwdlY2R3c Explanation for some of tables: answer_chk_results –

  • 0

My database structure looks like this:

https://docs.google.com/open?id=0B9ExyO6ktYcOenZ1WlBwdlY2R3c

Explanation for some of tables:

  1. answer_chk_results – checked answers table. So if some answer doesn’t exist on this table, it means it’s unchecked
  2. lesson_questions – lesson <-> question associations (by id) table

When I query database like this:

SELECT
    q.id,
    q.content,
    q.type
FROM
    `questions_and_exercises` q,
    `lesson_questions` lq
WHERE
    q.id = lq.qid
AND lq.lid = 1

I am getting all list of questions. But what I would like is a count of answers and checked answers for every question. When I use this query:

SELECT
    q.id,
    q.content,
    q.type,
    COUNT(DISTINCT a.ID) answer_count,
    COUNT(DISTINCT acr.id) checked_count
FROM
    `questions_and_exercises` q,
    `lesson_questions` lq
LEFT JOIN answers a ON a.qid = lq.qid,
LEFT JOIN `answer_chk_results` acr ON acr.aid = a.id
WHERE
    q.id = lq.qid
AND lq.lid = 1

The results is only 1 question with the wrong count of answers. (There are about 9-10 questions in my DB) What am I missing?

  • 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-10T16:47:36+00:00Added an answer on June 10, 2026 at 4:47 pm

    A group by would help, like:

    select  q.id
    ,       q.content
    ,       q.type
    ,       count(distinct a.id) as answer_count
    ,       count(acr.checked) as checked_count
    from    questions_and_exercises q
    join    lesson_questions lq
    on      q.id = lq.id
    left join
            answers a 
    on      a.qid = lq.qid
    left join
            answer_chk_results acr 
    on      acr.aid = a.id
    group by
            q.id
    ,       q.content
    ,       q.type
    

    Note that count(acr.checked) returns the number of answers with a row in the answer_chk_results table. And count(distinct acr.checked) returns the number of different values of the checked column. If checked is a boolean, that would always be one or zero.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My database structure looks something like this: Person Id Name FieldA FieldB Phone Id
I have a sql table in my database accounts whose structure looks like this.
The Wordpress database structure looks like this: wp_users (ID, user_registered, etc) wp_usermeta (user_id, meta_key,
I have a list of recipes obtained from a database that looks like this:
I have an XML structure that looks like this: <root> <index> <item>item 1</item> <item>item
Lets say that I have a database structure with three tables that look like
I have a simple database structure like this: entries table +----+-----------+------------+-----------+------+ | id |
I have a huge data set. The structure looks something like this: K_Field1, K_Field2,
I need to create MySQL tables that represent a tree structure like this: Root
this is my database structure for my terms table (categories): id | 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.