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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:28:30+00:00 2026-06-07T15:28:30+00:00

I have 3 tables comments(id,question_id, user_Id) // here the user_id is of the user

  • 0

I have 3 tables

comments(id,question_id, user_Id) // here the user_id is of the user who has asked the question
questions(id,q_desc, user_id)
users(id, name, rank)

A user can ask question and can comment on the questions.

I need a report in which I want to display every question with at most 3 top ranked user, who have commented on it, but the user who has asked the question should not be in the report for that particular question, but he too has the privilege to comment on his question.

EDited:::

Select * from comments inner join users(comments.user_id=u.id) group by question_id order by user.rank desc
  • 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-07T15:28:32+00:00Added an answer on June 7, 2026 at 3:28 pm

    It’s messy but it works:

    SELECT 
        a.question_id, a.user_id, a.name, a.rank
    FROM
    (
        SELECT a.*, b.name, b.rank
        FROM
        (
            SELECT DISTINCT b.question_id, b.user_id
            FROM questions a
            INNER JOIN comments b ON a.id = b.question_id AND a.user_id <> b.user_id
        ) a
        INNER JOIN users b ON a.user_id = b.id
    ) a
    INNER JOIN
    (
        SELECT a.question_id, b.rank
        FROM
        (
            SELECT DISTINCT b.question_id, b.user_id
            FROM questions a
            INNER JOIN comments b ON a.id = b.question_id AND a.user_id <> b.user_id
        ) a
        INNER JOIN users b ON a.user_id = b.id
    ) b ON a.question_id = b.question_id AND a.rank <= b.rank
    GROUP BY 
        a.question_id, a.user_id, a.name, a.rank
    HAVING 
        COUNT(1) <= 3
    ORDER BY 
        a.question_id, a.rank DESC
    

    EDIT: This produces the same results and is more succinct:

    SELECT a.*
    FROM
    (
       SELECT DISTINCT a.question_id, a.user_id, b.name, b.rank
       FROM comments a
       INNER JOIN users b ON a.user_id = b.id
    ) a
    INNER JOIN 
        questions b ON a.question_id = b.id AND a.user_id <> b.user_id
    INNER JOIN
    (
       SELECT DISTINCT a.question_id, a.user_id, b.rank
       FROM comments a
       INNER JOIN users b ON a.user_id = b.id
    ) c ON b.id = c.question_id AND a.rank <= c.rank
    GROUP BY 
        a.question_id, a.user_id, a.name, a.rank
    HAVING 
        COUNT(1) <= 3
    ORDER BY 
        a.question_id, a.rank DESC;
    

    These solutions also account for users that have posted more than one comment in the same question.

    See both solutions in action at SQLFiddle

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

Sidebar

Related Questions

I have two tables: COMMENTS and USERS . I save user's name on COMMENTS
I have these tables: comments uid id pid pages pid user users id rank
In my application I have two tables: Users and Profiles. A user has a
I have seen other questions asked on this topic, here on SO as well
I have these tables: USERS: user_ID username password email data_registered POSTS: post_ID user_ID post_title
I have two tables in my database. Comments CommentsID MembersID CommentsBy CommentsDesc CommentsActive CommentsDateEntered
I have five tables in my database. Members, items, comments, votes and countries. I
I have the following tables: posts (post_id, content, etc) comments (comment_id, post_id, content, etc)
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
Take tables: User, Comment, Snippet. A User can have many Snippets. A Snippet can

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.