SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    One-to-many relationship

    Hi,

    I'm struggling to get my head around how to structure a query for a profile page on my site.

    A basic version of the query is as follows -
    Code MySQL:
    SELECT `users`.`uid`
    , `users`.`name`
    , `users`.`email`
    , `user_groups`.`name` AS `group_name`
    , `user_groups`.`premium`
    FROM `users`
    INNER
      JOIN user_groups
    	ON user_groups.id = users.user_group_id								   
    WHERE `users`.`uid` = '{$uid}'
    LIMIT 1;

    This is working fine for now but I want to add a posts section on each profile. The posts table in basic form consists of -
    • pid (post id)
    • name
    • content
    • uid (user id)


    So I need a query that will loop through the posts table and get every post the user has made (which can be 0). Is it viable to use just 1 query here still or do I just need to do a simple query using "WHERE `uid` = '{$uid}' from the post table?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,263
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by goldkiller07 View Post
    Is it viable to use just 1 query here still
    absolutely, yes
    Code:
    SELECT users.uid
         , users.name
         , users.email
         , user_groups.name AS group_name
         , user_groups.premium
         , posts.pid
         , posts.name  AS post_name
         , posts.content
      FROM users
    INNER
      JOIN user_groups
        ON user_groups.id = users.user_group_id           
    LEFT OUTER
      JOIN posts
        ON posts.uid = users.uid
     WHERE users.uid = {$uid} -- note no quotes
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is, what happens when there are no posts to display? There will be no profile content with the current query.
    Last edited by goldkiller07; Jul 12, 2011 at 09:42. Reason: Changed what I thought

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,263
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    that's what the LEFT OUTER JOIN accomplishes -- it returns all the posts for a user, but if there aren't any, it still returns the user and his group
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay thanks for your help once again, I've figured out what I did wrong.


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
  •