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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:22:25+00:00 2026-05-30T00:22:25+00:00

In a database structure for Question/Answer/Comment like stachexchange projects, the database schema can be

  • 0

In a database structure for Question/Answer/Comment like stachexchange projects, the database schema can be three columns connected with FKs. This seems to be ideal, but I wonder what is the most efficient query to retrieve the data for a question. Answers will be simply retrieved and listed by a WHERE clause of the question_id.

My question is how to list comments nested under its corresponding answer? I am dealing with the PHP while loop to list answers and sub-lists of comments.

A very basic structure in my mind. A purpose of my question is to find idea database structure too; thus, this is merely a suggestion.

CREATE TABLE questions (
question_id int(11) NOT NULL AUTO_INCREMENT,
title varchar(255),
content text,
PRIMARY KEY (question_id);

CREATE TABLE answers (
answer_id int(11) NOT NULL AUTO_INCREMENT,
question_id int(11) REFERENCES questions(question_id),
content text,
PRIMARY KEY (answer_id);

CREATE TABLE comments (
coment_id int(11) NOT NULL AUTO_INCREMENT,
question_id int(11) REFERENCES questions(question_id),
answer_id int(11) REFERENCES answers(answer_id),
content text,
PRIMARY KEY (coment_id);
  • 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-05-30T00:22:27+00:00Added an answer on May 30, 2026 at 12:22 am

    Assuming a Table structure as follows(simplified)

    Questions Table
    QID | Question
    
    Answers Table
    AID | fQID | Answer
    
    Comments Table
    CID | fAID | comment
    

    fQID = foreign Key for Questions Table

    fAID = foreign key for Answers table

    Following query will select all the comments for a particular question

    select comment from question join answers on QUI=fQUID join comments on AID=fAID where QID='$questionID'
    

    Following query will select comments accourding to the Answer ID

    select aid, answer, comment from question join answers on QUI=fQUID join comments on AID=fAID where AID='$answerID'
    

    Edit:

    For Nesting comments under answers, use something like this (you will need to modify this, this is very rough)

    $sql = 'select comment, cid, aid, answer from question join answers on QUI=fQUID left join comments on AID=fAID where QID='$questionID' order by AID';
    $result = mysql_query($sql);
    
    $row = mysql_fetch_array($result);
    echo "<div>" . $row['answer'];
    $current = $row['aid'];
    do{
      if($current != $row['aid']){
        print "</div>\n<div>" . $row['answer'];
        $current = $row['id'];
      }
    
      $current = $row['aid'];
      if($row['cid'] != null){
        print "<div>" . $row['comment'] . "</div>";
      }
    }while($row = mysql_fetch_array($result));
    
    print "</div>";
    
    • 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've read every question and answer on this topic I can find on this
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have three types of questions: Vocab (represented by question:answer pairs) Grammar (represented by
This is a beginner's question but for some reason I cannot find the answer
Could someone give me an idea how to create this database structure. Here is
I know this question has been asked several times, but I would like to
I'm struggling to make a flexible data structure (like a relational database) for a
after getting my answer here: Database issue, how to store changing data structure i
When creating a database structure, what are good guidelines to follow or good ways

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.