Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2008
    Posts
    117

    Unanswered: Using joins with session variable

    Hi,
    I want to create a join with id from $userid.
    With my 2 tables,
    users
    id, username
    1 a
    2 b
    3 c
    friends
    id, userid, friendid
    1 1 2
    2 1 3
    3 3 2
    My session variable $userid holds a userid.
    Joins needs to get all friendid from friends table for the current userid, and for the corresponding friendid it needs to get username from users tables. I used this query but getting the wrong output.
    Code:
    SELECT friends.friendid,tbl_users.id,tbl_users.username 
    		  FROM friends,tbl_users
    		  WHERE tbl_users.id='$userid'

  2. #2
    Join Date
    Nov 2008
    Posts
    117
    Hi,
    This one is working for me. Does the syntax correct for this query?
    Code:
    SELECT id,username
    		  FROM tbl_users
    		  WHERE id in (SELECT friendid FROM friends WHERE userid='$userid')

  3. #3
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    Quote Originally Posted by bharanidharanit
    This one is working for me. Does the syntax correct for this query?
    If it runs then I guess the syntax is correct however it looks very ugly and will run slowly if you have a lot of data. Better to either use the following or look up the JOIN syntax:
    Code:
    SELECT id,username
    FROM   tbl_users u, friends f
    WHERE  u.id = '$userid'
           and f.userid = u.id

Posting Permissions

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