SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Combining Table Data

    I am trying to combine info from 2 tables, I have managed to output rep_Firstname & rep_Lastname, in the output but since I did this I have now lost all the other info from 'tblmembers'... Any ideas?

    PHP Code:
    $results mysql_query("SELECT tblrepresentatives.rep_Firstname,tblrepresentatives.rep_Lastname FROM tblmembers LEFT JOIN tblrepresentatives ON tblmembers.rep_NBR = tblrepresentatives.rep_NBR WHERE tblmembers.memberApproved = 'P'"); 


    if (
    mysql_num_rows($results) < 1) { 
    die(
    'No members were approved last month'); 


    else { 

      while (
    $qry mysql_fetch_array($results)) { 
          if (
    strtotime($qry['loginDateTime']) <= (time() + 86400*31)) { 
              
    $login .= 'Name: '.$qry["FirstName"].' '.$qry["LastName"].' ('.date("d/m/Y"strtotime($qry["JoinDate"])).') '.$qry["Email"].' '.$qry["rep_Firstname"].' '.$qry["rep_Lastname"].' ('.$qry["State"].') 
             '

          } 
      } 

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    If you're listing fields in the SELECT part of the statement, only those fields will be selected. If you want to select all the fields, there are several things you could do:

    SELECT tblrepresentatives.* FROM...

    This will select all the fields from the tblrepresentatives table.

    You could also do SELECT * FROM... which will select all the data from all the tables, but watch out for fields that have the same name - only one of the fields will be returned, and you can get unexpected results from that.

    Lastly, the other option you have is to list all the fields you want to retrieve.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried this

    PHP Code:
    $results mysql_query("SELECT tblrepresentatives.rep_Firstname,tblrepresentatives.rep_Lastname, [B]tblmembers.*[/B] FROM tblmembers LEFT JOIN tblrepresentatives ON tblmembers.rep_NBR = tblrepresentatives.rep_NBR WHERE tblmembers.memberApproved = 'P'"); 
    But now it doesn't display 'rep_Firstname' or 'rep_Lastname'... Any ideas?

  4. #4
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tomyknoker View Post
    I tried this

    PHP Code:
    $results mysql_query("SELECT tblrepresentatives.rep_Firstname,tblrepresentatives.rep_Lastname, [B]tblmembers.*[/B] FROM tblmembers LEFT JOIN tblrepresentatives ON tblmembers.rep_NBR = tblrepresentatives.rep_NBR WHERE tblmembers.memberApproved = 'P'"); 
    But now it doesn't display 'rep_Firstname' or 'rep_Lastname'... Any ideas?
    Because you've stated the table name it will be part of the returned name.. instead of 'rep_Firstname' you now have 'tblrepresentatives.rep_Firstname'.. you can fix this by using the 'AS' statment in your query -
    PHP Code:
    $query =    "SELECT tblrepresentatives.rep_Firstname as rep_Firstname, 
                tblrepresentatives.rep_Lastname as rep_Lastname, 
                tblmembers.* 
                FROM tblmembers 
                LEFT JOIN tblrepresentatives ON tblmembers.rep_NBR = tblrepresentatives.rep_NBR
                WHERE tblmembers.memberApproved = 'P'"


  5. #5
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by catweasel View Post
    Because you've stated the table name it will be part of the returned name.. instead of 'rep_Firstname' you now have 'tblrepresentatives.rep_Firstname'.. you can fix this by using the 'AS' statment in your query -
    PHP Code:
    $query =    "SELECT tblrepresentatives.rep_Firstname as rep_Firstname, 
                tblrepresentatives.rep_Lastname as rep_Lastname, 
                tblmembers.* 
                FROM tblmembers 
                LEFT JOIN tblrepresentatives ON tblmembers.rep_NBR = tblrepresentatives.rep_NBR
                WHERE tblmembers.memberApproved = 'P'"

    Hi catweasel... Ok I moved the tblmembers to the front and that seemed to work, but I don't really understand why was just playing around with different combos... Is it syntax(lly) correct? Or have I just got lucky?

    PHP Code:
    $results mysql_query("SELECT , tblmembers.*, tblrepresentatives.rep_Firstname,tblrepresentatives.rep_Lastname FROM tblmembers LEFT JOIN tblrepresentatives ON (tblmembers.rep_NBR = tblrepresentatives.rep_NBR) WHERE tblmembers.memberApproved = 'P'"); 


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
  •