我在another thread有一些關於在MySQL中從2個表中提取數據的問題上的幫助,看起來我需要內部聯接。內部聯接,關注和關注
我的表:
USERS
--id (int)
--username (varchar)
USER_FOLLOW
-- id (int)
-- follower (int)
-- user (int)
-- subscribed (current_timestamp)
查詢,$以下=
SELECT ufollower.id AS follower_id,
ufollower.username AS follower_name,
ufollowed.id AS user_id,
ufollowed.username AS user_name
FROM
/* JOIN twice against users, once to get the follower and once to get the followed */
user_follow
/* users aliased as ufollower to get the follower details */
JOIN users ufollower ON ufollower.id = user_follow.follower
/* users aliased as ufollowed to get the followed details */
JOIN users ufollowed ON ufollowed.id = user_follow.user
WHERE
user_follow.user = $p_id
p_id
是我要找的人的個人資料ID。
我需要顯示我關注的用戶名以及我關注的用戶名。我現在有一些原因的代碼表明我3次而不是3我關注的人:
while($apple = mysql_fetch_array($following)){
echo '<a href="'.$apple['user_name'].'">'.htmlspecialchars($apple['user_name']).'</a> ';
}
對不起,這個打開另一個線程,我一直盯着它幾個小時,我無法理解它。
您需要調試您在查詢中擁有的數據。 ''var_dump($ apple)'在你的循環中查看它包含的內容。您已經在'WHERE'子句中將其限制爲一個用戶標識。 –