我在另一个线程中获得了一些关于从 MySQL 中的 2 个表中提取数据的问题,看来我需要内部连接。
我的表:
USERS
--id (int)
--username (varchar)
USER_FOLLOW
-- id (int)
-- follower (int)
-- user (int)
-- subscribed (current_timestamp)
查询,$following =
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> ';
}
很抱歉打开另一个线程,我已经盯着它看了好几个小时,我无法理解它。