SELECT multiple table
i need some help here,
table "friends"
+------+-------------+-----------+
id | friend_id | user_id |
+------+-------------+-----------+
1 | 1222 | 99999 |
+------+-------------+-----------+
2 | 48989 | 1492 |
+------+-------------+-----------+
table "users"
+------+-------------+---------------+
id | user_name | user_image |
+------+-------------+---------------+
99999 | Mark | img/abc.jpg |
+------+-------------+---------------+
1222 | Tom | img/xyz.jpg |
+------+-------------+---------------+
etc. | etc. | etc.. |
+------+-------------+---------------+
i want SELECT table friends and make WHERE statement :
etc : ... WHERE user_id=$_SESSION[u开发者_Python百科ser_id] ... and will display data from table users
ok , let say : my current id is 99999 so in table friends is match only 1222 , so this will display all data(image,etc..) from id 1222(Tom) from table users.
So my question here is how i need to write this code for generate users data ?
*i try to use UNION and LEFT JOIN..but no luck..still newbie..
$user_id = intval($_SESSION['user_id']);
$friends_of_user = mysql_query('
SELECT
f.*, u.*
FROM
friends f
LEFT JOIN
users u
ON
u.id = f.friend_id
WHERE
f.user_id = '.$user_id);
and to exclude all users which doesn't have profiles in users table, just change LEFT JOIN
to JOIN
I'd use the following code:
$user_id = mysql_real_escape_string($_SESSION['user_id']);
$sql = "SELECT f.*
FROM users u
INNER JOIN friends f ON (u.user_id = f.friend_id)
WHERE u.user_id = '$user_id' ";
精彩评论