+ Reply to Thread
Results 1 to 3 of 3

Thread: Pulling from multiple tables...

  1. #1
    Barn Regular bryceowen is on a distinguished road bryceowen's Avatar
    Join Date
    Sep 2008
    Location
    Jacksonville, FL
    Posts
    93
    Rep Power
    4

    Pulling from multiple tables...

    Now this question is a bit from left field and I somehow doubt it will be possible, but here's what I'd like to do:

    I have two tables in my database; a log table and a users table. In the log table, I have four fields I retreive:
    Log - user,campaign,status,seconds

    In the users table, I only need one field:
    Users - full_name

    However, the users table has a 'user' field that is the same data as it is in the log. What I'm wanting to do is compare the user field in the log to the user field in the users table to pull the full_name for that user into my array. For example:
    PHP Code:
    sql="SELECT l.user,l.campaign,l.status,l.seconds,u.full_name FROM log l,users u WHERE l.user=u.user AND <date range or other filters>";
    $result=mysql_query($sql$link);
    while(
    $row=mysql_fetch_array($resultMYSQL_NUM)){
        
    $array[]=$row;
        if(!
    in_array($row[4], $user_array){
            
    $user_array[]=array$row[4];
        }

    Now this is an abbreviated version of what I have going on, but for this example, it'll suffice. This works in that it appends the full_name to the end of each entry the way I was hoping it would. HOWEVER! This method only works when l.user=u.user. There are also system events in the log that are logged as a special 'user' that doesn't appear in the users table. Ergo, when the query checks the log and sees these entries (where l.user<>u.user) it skips them. Problem is, I need them and adding the system 'user' to the users table isn't an available option (the user field is numeric and the system 'user' is alphanumeric).

    Is there some way to include the system log entries having them entered into $array with $row[5] being left blank?

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    This is more of a SQL question, so I'm moving this to the SQL Development forum.

    What you need is a LEFT JOIN, if I understand your logic correctly: SQL Join

    Hope this helps.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    Moderator don94403 is a jewel in the rough don94403 is a jewel in the rough don94403 is a jewel in the rough don94403's Avatar
    Join Date
    Mar 2008
    Location
    San Mateo, CA, USA
    Posts
    313
    Blog Entries
    8
    Real Name
    Don Ravey
    Rep Power
    6

    What you're dealing with here is fundamental, ground level relational database operations. When you have related data in several tables, you absolutely MUST have primary and foreign key fields. Then you can use Joins and do other wonderful things. Try not to confuse unrelated matters, like whether or not you want to display something as output. The only things that matter are that a primary key must be UNIQUE within its table, and there must ALWAYS be a matching primary key for EVERY foreign key.

    In your case, it seems like you ALMOST have these conditions, the part that's missing is this 'special' user that doesn't have a matching user record. That's pretty simple to fix. You will have to reconcile the data type differences, but that's not really much of an issue. One easy solution would be to change the numeric user code to be alphabetic, then you can include the 'special' user in the user table (even if there's no other data there but the ID). That won't work, of course, if the user ID is an auto-increment number. If so, you'll have to do something like assign some number as the 'special user' ID and fudge some code in a few places to use it instead of the alphabetic user identification now being used. These are no more than annoyances, they're not serious issues.

    Please understand this: when working with a relational database, you absolutely MUST follow the rules that apply, you can't "cheat" and do little workarounds. You have to THINK in relational database terms. When you do, everything will work beautifully and you will be able to expand your database in the future, if needed.

    Once you have your primary and foreign keys fixed, you can use either of the SQL syntax forms:
    Code:
    SELECT * FROM table1 AS a, table2 AS b WHERE a.ID = b.ID
    
    or
    
    SELECT * FROM table1 LEFT JOIN table2 ON a.ID = b.ID
    
    Do read the W3Schools reference link that JMH provided for you.

+ Reply to Thread

Similar Threads

  1. Print Multiple Documents in one go
    By noFriends in forum ASP Development
    Replies: 14
    Last Post: September 30th, 2008, 09:24 AM
  2. Form for multiple tables and queries
    By nboscaino in forum Microsoft Access
    Replies: 1
    Last Post: August 21st, 2008, 07:55 PM
  3. Multiple Updates
    By Shem in forum .NET Development
    Replies: 4
    Last Post: July 8th, 2008, 02:29 PM
  4. Preventing Multiple Logins
    By richyrich in forum .NET Development
    Replies: 17
    Last Post: July 4th, 2008, 05:33 PM
  5. Permissions on Tables, Stored Procedures, etc.
    By theChris in forum Microsoft SQL Server
    Replies: 2
    Last Post: March 24th, 2008, 12:49 PM

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

SEO by vBSEO