0

我有以下可能与此查询相关的 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 专家,所以如果你能提供一个可能也有用的答案,那就太好了。我可以做简单的查询,但是在加入表时,我不知道发生了什么。

4

1 回答 1

1

基本上 user_follow 表连接另外两个用户,一个跟随另一个用户,这个查询获取这两个用户的一个特定关注。

这个查询解释可以描述为:
对于 id 为 :id 的 user_follow,获取一个正在关注另一个用户(ufollowing)的用户(ufollower)及其 id 和用户名。

于 2013-05-27T18:54:12.680 回答