SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Netherlands
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Select from 2 tables, even when there nothing in table 2

    Hi,

    got two tables, IDs are matching.

    table1
    ======
    ID

    table2
    =======
    ID
    name

    SELECT FROM `table1`,`table2` WHEN
    table1.ID = table2.ID

    But, it should also select the ids from table1 when there are no records in table 2 with the same ID, how to do that?

    thanks in advance

  2. #2
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,200
    Mentioned
    105 Post(s)
    Tagged
    1 Thread(s)
    Use the LEFT JOIN syntax. If there's nothing corresponding in table2, the fields will be null.

    SELECT table1.ID
    , table1.Field1
    , table1.Field2
    , table2.Field3
    , table2.Field4
    FROM table1 LEFT JOIN table2 ON table1.id = table2.id
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style

  3. #3
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Netherlands
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, that did the trick, for some reason I still cannot grasp the join thingie in mysql.


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
  •