我有以下可能与此查询相关的MySQL表:
Table: user_follow
Columns: | Type:
-----------|---------
id | INT(11)
follower | INT(11)
user | INT(11)
subscribed | INT(11)
ID是跟随者的ID,跟随者是关注用户的人,订阅者是跟随他们的人。关注者和用户将是用户表中人员的ID:
Table: users
Columns: | Type:
----------|---------
id | INT(11)
username | INT(11)
这不是完整的用户表,但对于MySQL查询所做的任何事情都应该足够了。 users表中的ID是用户的ID,用户名是用户名。所以这是我试图弄清楚它在做什么的查询。我相信它正试图从用户的数据库中获取用户名:
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的专家,所以如果你能提供一个可行的答案,那就太好了。我可以做简单的查询,但是当谈到加入表时,我不知道发生了什么。
答案 0 :(得分:1)
基本上,user_follow表连接另外两个用户,一个跟随另一个用户,此查询为一个特定的跟随者获取这两个用户。
解释的这个问题可以描述为:
对于id为id的user_follow,获取一个跟随另一个用户(ufollowing)的用户(ufollower)及其id和用户名。