If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > Using joins with session variable

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-27-10, 04:13
bharanidharanit bharanidharanit is offline
Registered User
 
Join Date: Nov 2008
Posts: 115
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'
Reply With Quote
  #2 (permalink)  
Old 01-27-10, 04:23
bharanidharanit bharanidharanit is offline
Registered User
 
Join Date: Nov 2008
Posts: 115
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')
Reply With Quote
  #3 (permalink)  
Old 01-27-10, 04:53
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On