我有以下可能与此查询相关的 MySQL 表:
Table: user_follow
Columns: | Type:
-----------|---------
id | INT(11)
follower | INT(11)
user | INT(11)
subscribed | INT(11)
ID是follow的ID,follower是关注用户的人,subscribed是他们关注的时间。follower 和 user 将是 users 表中人员的 ID:
Table: users
Columns: | Type:
----------|---------
id | INT(11)
username | INT(11)
这不是完整的 users 表,但对于这个 MySQL 查询正在做的任何事情都应该足够了。users表中的ID就是用户的ID,username就是用户名。所以这是我试图弄清楚它在做什么的查询。我相信它正在尝试从用户的数据库中获取用户名:
SELECT `ufollower`.`id` AS follower_id, `ufollower`.`username` AS follower_name,
`ufollowed`.`id` AS user_id, `ufollowed`.`username` AS user_name
FROM `user_follow`
JOIN users ufollower ON `ufollower`.`id` = `user_follow`.`follower`
JOIN users ufollowed ON `ufollowed`.`id` = `user_follow`.`user`
WHERE `user_follow`.`user` = :p_id
我试图运行的完整代码是这样的(包括查询):
//Get people who this person is following
$following = $db->prepare("SELECT `ufollower`.`id` AS follower_id, `ufollower`.`username` AS follower_name, `ufollowed`.`id` AS user_id, `ufollowed`.`username` AS user_name FROM `user_follow` JOIN users ufollower ON `ufollower`.`id` = `user_follow`.`follower` JOIN users ufollowed ON `ufollowed`.`id` = `user_follow`.`user` WHERE `user_follow`.`user` = :p_id");
$following->bindValue(":p_id",$p_id,PDO::PARAM_STR);
$following->execute();
$following = $following->fetchAll(PDO::FETCH_ASSOC);
var_dump($following);
//If I do $following = $following->fetch(); and var_dump that, it returns "bool(false)" which I learned the other day that it couldn't do the query or there were no results found or something
另外,我不是 MySQL 专家,所以如果你能提供一个可能也有用的答案,那就太好了。我可以做简单的查询,但是在加入表时,我不知道发生了什么。