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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:29:26+00:00 2026-06-07T05:29:26+00:00

I am making a QA site, not unlike SO. I would like to list

  • 0

I am making a QA site, not unlike SO. I would like to list all of the questions that are in my database, count up the number of votes and the number of answers for each question, and then find out whether a particular user has already voted for each question. In the same query.

Let me back up and give a bit of context. I have the following SQL tables:

qa_questions => questionid    title    text
qa_votes => voteid    questionid    userid
qa_answers => answerid    text    questionid    userid

The first is a table of questions, which have numerical ids, titles, and text. Users can vote for questions. There are only upvotes. Each user can vote for as many questions as (s)he likes, but only once for each question. Whenever a user votes for a question, an entry is added to the votes table, recording the quesitonid and userid. The answers table stores a numerical id and text for each answer, and also references the question that is being answered and the user who submitted the answer.

To get a list of all questions, counting the number of votes for each question, and showing whether a specific user (e.g. user #1) has voted for the question, I do:

SELECT qa_questions.questionid AS qid, title, text, COUNT( voteid ) AS num_votes,
MAX(CASE WHEN qa_votes.userid=1 THEN 1 ELSE 0 END ) AS voted_for
FROM qa_questions
LEFT JOIN qa_votes ON qa_questions.questionid = qa_votes.questionid
GROUP BY qa_questions.questionid

This works. An example of what this returns:

qid    title            text                                                num_votes    voted_for
1      What is 2 + 2?   I want to know the answer to this math question!    2            1

(where the 1 in the voted_for column stands for TRUE, and indicates that the current user has voted for this question).

I can also find out how many answers each question has, by doing this:

SELECT qa_questions.questionid AS qid, COUNT( answerid ) AS num_answers
FROM qa_questions
LEFT JOIN qa_answers ON qa_questions.questionid = qa_answers.questionid
GROUP BY qa_questions.questionid

which returns:

qid  num_answers
1    1
2    2
3    0
4    3

Okay, so far so good. Now I want to combine these two tables, so I simply to a join on qid:

SELECT t1.qid, title, TEXT, num_votes, voted_for, num_answers
FROM (
    SELECT qa_questions.questionid AS qid, title, text, COUNT( voteid ) AS num_votes,
    MAX(CASE WHEN qa_votes.userid=1 THEN 1 ELSE 0 END) AS voted_for
    FROM qa_questions
    LEFT JOIN qa_votes ON qa_questions.questionid = qa_votes.questionid
    GROUP BY qa_questions.questionid
) AS t1
JOIN (
    SELECT qa_questions.questionid AS qid, COUNT( answerid ) AS num_answers
    FROM qa_questions
    LEFT JOIN qa_answers ON qa_questions.questionid = qa_answers.questionid
    GROUP BY qa_questions.questionid
) AS t2
ON t1.qid = t2.qid

Ummmm …. yuck. This works, but I can’t help feeling that it is much uglier than it needs to be … is there a way that I can simplify this?

  • 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-07T05:29:29+00:00Added an answer on June 7, 2026 at 5:29 am

    You can simply “stack” these joins… This will be drastically more performant.

    SELECT 
      qa_questions.questionid AS qid, 
      title, 
      text, 
      COUNT(DISTINCT voteid ) AS num_votes,
      MAX(CASE WHEN qa_votes.userid=1 THEN 1 ELSE 0 END) AS voted_for, 
      COUNT(DISTINCT answerid ) AS num_answers
    FROM qa_questions
    LEFT JOIN qa_votes ON qa_questions.questionid = qa_votes.questionid
    LEFT JOIN qa_answers ON qa_questions.questionid = qa_answers.questionid
    GROUP BY qa_questions.questionid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a web site that users can build their TV watch list. They
I'm making a site like StackOverflow in Rails but I'm not sure if it's
I am making a site that links an unlimited number of physical addresses to
I'm making a Spree site that has links for changing the number of products
I am making n alcoholic site that has a gateway for age verification wish
Iphone changes some fonts making the site unproportional and not as originally designed. Is
I have a web-site based on PHP, to which I would like to add
I'm making a site where the user can select a rental property that has
I'm making a site that works flawless in Chrome and Firefox. But in Safari
I am making a site that consumes a csv file this file can come

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.