Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts

    help with a join query

    Here's the query what's causing me some trouble.

    Code:
    SELECT ar.ID as aID, ar.username as auser, ar.refID, m.user_id,m.username as muser 
    FROM ag_refered ar 
    LEFT JOIN members m ON ('m.user_id' = 'ar.refID')
    Ive checked the database and in ag_refered there is a record with the values
    ID = 1
    username = SomeUser
    refID = 96

    and in the members table i have a record
    user_id = 96
    username = testRefer

    yet when i run the query in something like PHPMyAdmin i get this

    aID | auser | refID | user_id | muser
    1 | SomeUser | 96 | NULL | NULL



    Can anyone see why?

    EDIT: Both tables are in the same database and the correct database is defined in my connection. If also tried the ON ('m.user_id' = 'ar.refID') like ON ('ar.refID' = 'm.user_id') too and i get the same reuslt
    Last edited by tomharto; 10-02-2011 at 06:41 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,537
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Because as far as mySQL is concerned, you're joining where two (different) strings are the same. By putting it in quotes, its interpreting m.user_ID and ar.refID as strings, so...

    Code:
    SELECT ar.ID as aID, ar.username as auser, ar.refID, m.user_id,m.username as muser 
    FROM ag_refered ar 
    LEFT JOIN members m ON m.user_id = ar.refID


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •