SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Complex SELECT statement help - is it possible?

    Ok..I have a many to many relationship and I want to look at one table, one row at a time, and print all values for each row. However, for each row, if there a relationship has been found in another table, I want to print that tables relationship data also....briefly:

    scan table1, 1 row at a time;
    if table2.valueid = table1.value {
    print table1.allvalues and table2.allvalues
    }
    else print table1.allvalues

    My question is this: Is this possible through solely using a SELECT statement?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    SELECT table1.*, table2.*
    FROM table1
      LEFT OUTER JOIN table2
      ON table2.valueid = table1.value
    The columns for table2 would simply be NULL if there are no matching rows.

  3. #3
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    please provide sample rows from both tables, plus your desired output.

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After thinking about it, it will probably be too complex. i have 3-4 tables that I have to reference.

    I have a news feed website I am working on....it has: stories, photos, files, links tables all within one database. Basically i am trying to export all the stories data, including all related stories, files, photos, etc, into xml tree format. I guess I am stuck writing a script to do what I want.

    I have been testing the script, but cant seem to get the nested array loops to work, hence trying to find an alternative solution.

    If you feel inspired, please check out my dilemma at this thread:

    http://www.sitepoint.com/forums/showthread.php?t=488354


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
  •