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

  • Home
  • SEARCH
  • 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 7991627
In Process

The Archive Base Latest Questions

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

My question is: How to get different values in one sql query? I am

  • 0

My question is: How to get different values in one sql query? I am trying this:

SELECT 
          q.id,
          q.question_title,
          SUM(op.option_value) AS total_votes,
          COUNT(op.option_value) AS number_votes,
          tab.comments 
FROM
          questions_options AS op 
          INNER JOIN questions AS q 
                    ON (q.id = op.q_id) 
          INNER JOIN 
                    (SELECT 
                              SUM(p.unseen) AS comments 
                    FROM
                              questions_options AS p) tab 
                    ON (q.id = tab.q_id) 
WHERE op.option_value > 0 
GROUP BY q.question_title 

This query is working perfectly, If i remove the middle “INNER JOIN” (i.e shows me the id, question title, sum of total votes rating, and the number of votes….

But I want to show unseen comments also, which is another field in that table and takes “1” for unseen (unread) comment…

So how can I do this by putting unseen comments number….any idea or suggestions…

  • 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:20:43+00:00Added an answer on June 4, 2026 at 1:20 pm

    I think Judge Mental is right.

    You should SELECT the “q_id” field in your subquery to be able to make an inner join to “tab.q_id”.

    -Explanation-

    Your first INNER JOIN looks like this:

    SELECT * FROM questions_options AS op INNER JOIN questions AS q ON (q.id = op.q_id)

    ..so basically the relation (and join) between your two tables is based on
    questions.id and questions_options.q_id.

    Well, it’s the same story for you subquery.

    In your subquery you define a SELECT statement FROM “questions_options”,
    which will only return the columns/fields you put after your SELECT.

    (SELECT SUM(p.unseen) AS comments FROM questions_options AS p) tab

    To be able to use any data from you subquery, you will have to SELECT the
    questions_options.q_id field and make a join with the questions.id field already
    in your main query.

    (SELECT p.q_id, SUM(p.unseen) AS comments FROM questions_options AS p) tab
    INNER JOIN (...subquery...) ON (q.id = tab.q_id)

    So tab.q_id refers to the p.q_id you selected in your subquery.

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

Sidebar

Related Questions

Question: I can get the SQL Server database language by querying: SELECT @@language And
This is the same question as: How to dynamically generate SQL query based on
HTML Javascript question to get the selected value of a input-select I can use
I have a route defined like this: GET /question/:q_id controllers.Questions.viewQuestion(q_id: Long) Then in my
I'm using Alberto Santini's solution to this question to get a spiral grid reference
Very basic question - how to get one value from a generator in Python?
Hopefully the last NN question you'll get from me this weekend, but here goes
I have asked this before but I didn't get the question right so the
Yet another IE is doing something different from other browsers question but this is
One of my last questions came down to this topic and I didn't get

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.