Retrieving Information From Two Tables

Discussion in 'Databases' started by Alley Cat, Jun 10, 2007.

  1. #1
    I have information in two tables in a database, which I would like to incorporate into 1 table on a web page. Within the database tables there are common aspects, 1 being email fields in both. How can I join these two tables and retrieve the information I require in the web page table?
     
    Alley Cat, Jun 10, 2007 IP
  2. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #2
    select table1.*, table2.* from table1 left join table2 on table1.email = table2.email
     
    gibex, Jun 11, 2007 IP
  3. link_dev

    link_dev Peon

    Messages:
    292
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    In the following query you may get certain columns twice which are repeated twice in two tables. So, select columns individually like
    select table1.a, table1.b, table1.c, table1.emailID, table2.a1, table2.b1 from
    table1 inner join table2 on table1.emailID = table2.emailID

    Also, choose inner join or left/right join depending on what you want.
    If you want all rows from table1 irrespective of a matching email id in table2 then use 'left join'
    or
    If you want all rows from table2 irrespective of a matching email id in table1 then use 'right join'

    But, if you want only matching rows then use INNER JOIN.

    Hope this helps

     
    link_dev, Jun 11, 2007 IP
  4. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #4
    yep, good point :)
     
    gibex, Jun 11, 2007 IP
  5. Alley Cat

    Alley Cat Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for your invaluable assistance guys, that code got the script working perfectly. If you send me a PM through here, I'll show you my appreciation by sending the links to the 5 albums that my friend and I have recorded. The recordings play for six and a half hours, so should be something in there that you will enjoy.
     
    Alley Cat, Jun 11, 2007 IP
  6. link_dev

    link_dev Peon

    Messages:
    292
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It shouldn't be $row['upload_id'], but $result['upload_id']
    Correspondingly change that in all places.
    --link_dev



    <td align=\"left\"><img src=\"pictures/{$row['upload_id']}\"/></td>
    <td align=\"left\">" . stripslashes($row['parkname']) . "</td>
    <td align=\"center\">" . stripslashes($row['parklocation']) . "</td>
    <td align=\"left\">" . stripslashes($row['caravandetails']) . "</td>
    </tr>\n";

     
    link_dev, Jun 11, 2007 IP
  7. Clark Kent

    Clark Kent Guest

    Messages:
    122
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you could write error, It will be better to analyze.
    but I can't see while loop in that code. In above code you are executing query than using row$['fields'] but where did you define $row?

    Try something like this;

    $result = mysql_query ($query);
    while ($row = mysql_fetch_array($result)) {
    $ip = $row["upload_id"];
    ...
    ...
    ...
    ...
    }


    Edit: I am a little bit late, link_dev answered first :) If your query returns 1 record you don't need a while loop as I write.
     
    Clark Kent, Jun 11, 2007 IP