Join Three tables

Discussion in 'Databases' started by bumbar, Jul 18, 2009.

  1. #1
    I'm need some help with some sql to return some data...obviously ;)

    I have 3 tables. tbl_users, tbl_info, and tbl_images.

    I want to connect two tables tbl_info and tbl_images with tbl_users. The relationship with their is id_user

    Here is a simplified exmaple of table setup:

    tblusers
    id_user username
    ----------
    1 bob
    2 peter

    tbl_info
    id_info id_user description
    ----------
    1 1 some desc
    2 2 other desc

    tbl_images
    id_image id_user
    ----------
    1 1 img1
    2 2 img2


    I tried this code
    
    SELECT tbl_users.username, tbl_info.description, tbl_images.name FROM `tbl_users` JOIN users_info ON (tbl_users.id_user = tbl_info.id_user) JOIN tbl_images ON (tbl_users.id_user = tbl_images.id_image) 
    
    PHP:
    But not work.
    how to solve the problem?

    Thanx!
     
    bumbar, Jul 18, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Try this:

    SELECT tbl_users.username, tbl_info.description, tbl_images.name
    FROM `tbl_users`
    LEFT JOIN tbl_info ON tbl_users.id_user = tbl_info.id_user
    LEFT JOIN tbl_images ON tbl_users.id_user = tbl_images.id_user

    You need to double check your column names. They are inconsistant in your initial query.
     
    jestep, Jul 18, 2009 IP
  3. shaibibutt

    shaibibutt Member

    Messages:
    606
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    35
    #3
    shaibibutt, Jul 20, 2009 IP