如何从mysql查询中获取关注者/以下列表

时间:2015-12-17 06:55:58

标签: mysql

这是我的关注者/以下数据库表架构。 我想获取特定用户的所有数据。例如user_id 6 跟随 7,8,9 ,然后是 7 。我的目的是找出该特定用户是否关注他的粉丝。我怎样才能做到这一点?

public class RegistrationData {
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required]
    public MyUser User { get; set; }    // MyUser : IdentityUser

    //blah blah, more fields

    public List<UserTask> Activitys { get; set; }
}

public class UserTask {
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [Required]
    public bool IsDone { get; set; } = false;

    [Required]
    public int RegistrationId { get; set; }
    [Required]
    public RegistrationData Registration { get; set; }

    [Required]
    public int TaskId { get; set; }
    [Required]
    public Task Task { get; set; }

    public List<UserResponse> Responses { get; set; }
}

public class Task {
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.None)] // ID comes from loaded config
    public int Id { get; set; }

    [StringLength(20, MinimumLength = 1)]
    public string Name { get; set; }

    [Required]
    public int Ordinal { get; set; }

    [Required]
    public int GroupId { get; set; }
}

public class UserResponse {
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [Required]
    public int UserTaskId { get; set; }
    [Required]
    public int QuestionNumber { get; set; }
}

4 个答案:

答案 0 :(得分:5)

您可以use separate queries

获取关注者

SELECT GROUP_CONCAT(DISTINCT t1.follower_id) as followers FROM test_f t1 WHERE t1.user_id = 6

跟随

SELECT GROUP_CONCAT(DISTINCT t2.user_id) as following FROM test_f t2 WHERE t2.follower_id = 6

答案 1 :(得分:4)

我添加了dummy table data here

您将获得每个用户的关注者列表:

Chrome Custom tabs

结果如:

enter image description here

您可以在同一查询中获得关注者和关注者。

SELECT user_id, GROUP_CONCAT(DISTINCT follower_id SEPARATOR ', ') as following 
FROM followers GROUP BY user_id

enter image description here

答案 2 :(得分:1)

因此,对于给定的表 tmp

return value

以下是查询:

id     |   user_id   |  follower_id
------------------------------------
1            6             7
2            6             8
3            7             6
4            8             15
5            6             9
6            5             7  
7            9             6
8            15            8

这将给出一个相互关注的列表,最后的where子句只是确保我们获得“用户-关注者”相互关注的唯一记录。

输出:

select t1.user_id, t1.follower_id
from tmp t1
join tmp t2
on t1.user_id = t2.follower_id
and t1.follower_id = t2.user_id
where t1.user_id < t1.follower_id

答案 3 :(得分:0)

你可以尝试这种方式,它适用于我粉丝跟随

$Followers = mysqli_query("SELECT id FROM followers WHERE  follower_id ='$_SESSION[id]'");
echo "Followers :".mysqli_num_rows($Followers);


$Following = mysqli_query("SELECT id FROM followers WHERE user_id ='$_SESSION[id]'");
    echo "Followers :".mysqli_num_rows($Followers);

$ _ SESSION [id] 是您的身份

...

如果您想查看其他用户个人资料不是您的个人资料

$Following = mysqli_query("SELECT id FROM followers WHERE  follower_id ='$_GET[u_id]'");
echo "Following :".mysqli_num_rows($Following);

$Following = mysqli_query("SELECT id FROM followers WHERE  user_id ='$_GET[u_id]'");
    echo "Following :".mysqli_num_rows($Following);

$ _ GET [u_id] 您查看的用户ID