SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard mPeror's Avatar
    Join Date
    Mar 2005
    Location
    Saudi Arabia
    Posts
    1,724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation How do you query this field? (simple questions)

    Say I have those three tables for a basic forum:
    Thread:
    - id [PK]
    (and a few other columns)

    user_thread:
    - id [PK]
    - thread_id [FK1]
    - user_id [Fk2]

    User:
    - id [PK]
    - name
    (and a few other columns)

    My questions here are basically:

    1) When I'm working with the "Thread" table in a query, what's the best way to fetch user name associated with that "Thread"? or should that be in a different query?

    2) Am I doing the relations correctly? to be honest, I don't know when I should have an intermediate table between related tables, I just guessed that.


    Your help is much appreciated
    Last edited by mPeror; Apr 6, 2008 at 05:35.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    1) with a join query

    2) you need an intermediate table when the relationship is many-to-many

    where is the actual post?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard mPeror's Avatar
    Join Date
    Mar 2005
    Location
    Saudi Arabia
    Posts
    1,724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh sorry Rudy. I got things mixed up there. I've edited now to show what I'm trying to do properly.

    As you can see, this relation is one-to-many (every user has 0 or more threads), does that mean there shouldn't be an intermediate table?

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    okay, your user table is fine, and the thread table is maybe fine

    where is the actual post? and is there a relationship between thread and post?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Wizard mPeror's Avatar
    Join Date
    Mar 2005
    Location
    Saudi Arabia
    Posts
    1,724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I apologize again for being vague. I've changed it now. The relation is between the thread and the user who posted the thread.

    I'm using those table as an example only. They're not the real table, because the actual tables are too big.

    So, what do you think? is the intermediate table fine here?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •