+ Reply to Thread
Results 1 to 7 of 7

Thread: MySQL Join

  1. #1
    jason32 is offline x10Hosting Member jason32 is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    5

    MySQL Join

    I have a bit of an understanding of how a join works in a SQL query, but it's really basic and I can't quite get my head around it enough to figure out how to do what I need.

    Here's the situation. I have two tables, one named pages which has columns (amongst others) called create_id and modified_id and a second table named uses which has id as a key and a column (amongst others) called realname.

    What I want to do is SELECT * from pages, but in addition to create_id and modified_id I also want to look these id's up in the users table and return the realname that corresponds to each of these id's as well.

    I hope my question makes sense. Thanks in advance for your help, and let me know if I'm not being clear enough :happysad:
    Edit:
    I should probably add that I'm hoping to do this with just one SQL query if I can.
    Last edited by jason32; 07-27-2009 at 10:57 PM. Reason: Automerged Doublepost

  2. #2
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: MySQL Join

    Yes this is possible, and thanks for being so clear. Most people don't understand how to ask for help

    This a query that should work for you:
    Code:
    SELECT * FROM pages
    INNER JOIN uses ON id = create_id OR id = modified_id;
    Tell me if this works for you. If you have any preference in how the results should be sorted, then post back.

    Currently, this will return everything from both tables when there is a match between either table IDs. You will probably want to be a bit more concise.
    Last edited by Twinkie; 07-27-2009 at 11:06 PM.

  3. #3
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: MySQL Join

    Think you have to use a three table join, pages with uses with uses

    SELECT [fill in pages cols] , usersOne.realname, usersTwo.realname
    FROM pages, usersOne uses , usersTwo uses
    WHERE create_id = usersOne.id AND modified_id = usersTwo.id

    Note: you said the second table was "uses" and that is what I used. Did you mean "users"?

    Also assumed create_id and modified_id are not Null
    Last edited by descalzo; 07-28-2009 at 09:23 PM.

  4. #4
    jason32 is offline x10Hosting Member jason32 is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    5

    Re: MySQL Join

    Thanks so much for your help!

    Twinkie: the query works great, but assuming I'm looking for just one record from pages (I almost always am in this application) it returns either one or two rows, depending on whether the user that created the page is also the user that last modified it. This is fine and it's certainly a better solution that what I'm doing right now (three MySQL queries in the same script) but would the two rows returned always be in a particular order (id = create_id first, then id = modified_id next) or will I have to return the id column also and test for which row is which in the PHP script?

    descalzo: This seems like it would be the perfect solution for me - one row returned with all the information I need in it - but when I try running the query I get a SQL error back saying "Not unique table/alias: 'users'

    You're assumptions are good ones though. create_id and modified_id are never null, and the id contained within them always exists in the users table. Also I did mean 'users' rather than 'uses.' Typo :happysad:

    Thanks again for your help guys!

  5. #5
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: MySQL Join

    Code:
     
    SELECT * 
    FROM `pages` AS p
    JOIN `users` AS u
    JOIN `users` AS uu
    WHERE p.id_create = u.id
    AND p.id_modify = uu.id
    I got this to work from phpAdmin on a test mySql db.
    Last edited by descalzo; 07-29-2009 at 01:59 PM.

  6. #6
    jason32 is offline x10Hosting Member jason32 is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    5

    Re: MySQL Join

    Thanks descalzo, that's awesome. It does exactly what I was trying to achieve!

  7. #7
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: MySQL Join

    The ON clause is supposed to replace the WHERE clause in JOIN statements?

    The correct way to do this can be achieved with only one join, but it doesn't matter if it works.

+ Reply to Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. Which is better postgresql or MySQL?
    By Kurai Kumo in forum Scripts & 3rd Party Apps
    Replies: 11
    Last Post: 12-10-2009, 08:17 AM
  3. Replies: 14
    Last Post: 09-29-2008, 07:07 PM
  4. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  5. Have a problem with my forum
    By tikloos in forum Scripts & 3rd Party Apps
    Replies: 43
    Last Post: 01-19-2006, 01:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers