new mysql syntax encountered...

Discussion in 'MySQL' started by mvallar, Jun 2, 2007.

  1. #1
    Hi,

    I'm reading thru a book and came across this piece of code:

    $verify = "select ft.topic_id, ft.topic_title from
    forum_posts as fp left join forum_topics as ft on
    fp.topic_id = ft.topic_id where fp.post_id = $_GET[post_id]";

    I know it's comparing two tables and some fields, but I don't understand
    how it works. I would appreciate it if someone could translate it into English
    so that I can see what's going on.... Thanks, mvallar
     
    mvallar, Jun 2, 2007 IP
  2. turiel

    turiel Peon

    Messages:
    148
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You're going through the data in forum_posts and forum_topics and matching all the rows where the fields forum_posts.topic_id is equal to forum_topics.post_id and where the forum_posts.post_id is equal to the posted data.
     
    turiel, Jun 2, 2007 IP
  3. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Each topic can have a number of posts (one to many relationship), the way for the script to know which topic a post belongs to is by using IDs. Each topic has an ID, and each post has a database field corresponding to a topic ID. So then, in the query, you can select the post and the associated topic by finding one with matching IDs (comparing the post_id field in both tables)
     
    krt, Jun 2, 2007 IP
  4. mvallar

    mvallar Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    turiel and krt - thanks guys! I appreciate the input. Your explanations have helped me understand this code.

    best regards,

    mikev
     
    mvallar, Jun 3, 2007 IP