views:

56

answers:

1

Hi,

I am learning mysql join queries. To pratice, I decided to make tables for a website like stackoverflow. I made three tables basically.

Question_Thread
    thread_id
    title
    username
    datetime
Question_Reply
    reply_id    
    thread_id
    text
Question_Text_Comment 
    comment_id
    reply_id
    comment

How do I query all the replies and comments of each reply? I can't find a relationship here. It is not many-to-many, one-to-many, or one-to-one. It looks like one-to-many because one reply have many comments. The problem is that question detail page have many replies.

please advise me...

+2  A: 

Question and reply is a one-to-many relationship. Reply and comment is a one-to-many relationship.

The properties (and keys) as far as I can see are looking good. I don't see the problem...

_NT
so..do I have to query each reply's comments on question detail (the page you are viewing right now)? for example, if there are three replies and each of them have four comments. Do i have to query three times?
Moon
No you don't have to. You can use an ORM to facilitate this, such as NHibernate, or Linq-to-Sql (which is what StackOverflow use), or other. This would allow you to do load the Question and its properties would contain the required data.But if you're using bare SQL queries, then yes, you would query three (or more) times. 1) Get Question, 2) Get Question's Replies, 3) Get all Comments for all retrieved Replies. Mind you, this is what ORM would do anyway, it is just abstracted for you not to worry to much about.
_NT
ah! I thought there is a query that queries all replies and comments.
Moon