简体   繁体   English

在 mysql 中存储关注、关注、喜欢数据的最佳方式是什么

[英]What is the best way of storing following,followers,likes data in mysql

Hello I want to create social media app.I am using PHP+MYSQL for my back-end.您好,我想创建社交媒体应用程序。我在后端使用 PHP+MYSQL。 I have standard user columns and followers,following,posts and followers_id,following_id,posts_id columns in MYSQL users table and I separate all ids with comma.For example我在 MYSQL 用户表中有标准的用户列和关注者、关注、帖子和关注者 ID、关注 ID、帖子 ID 列,我用逗号分隔所有 ID。例如

followers 7 , following 2 , followers_id ,42,43,47,48,49,50,51 following_id 12,14 Sometimes i struggle with PHP explode() function but it works well.追随者7 ,追随者2 ,followers_id ,42,43,47,48,49,50,51 following_id 12,14有时我与 PHP explode() function 斗争,但它运作良好。 So is it good way to store and find users like this.那么存储和寻找这样的用户的好方法吗? Do you have any suggestion?你有什么建议吗? What will happen if someone follows 1000 users.In this case my PHP Script will fetch all data (maybe it will cause for RAM problems)如果有人关注 1000 个用户会发生什么。在这种情况下,我的 PHP 脚本将获取所有数据(可能会导致 RAM 问题)

I think a good way is using mysql tables where fields are related by id.我认为一个好方法是使用 mysql 表,其中字段与 id 相关。 Relational database allows you to query on multiple tables to obtain much interesting data like: who follow who, how many followers, followers in common with other users.关系数据库允许您在多个表上查询以获得许多有趣的数据,例如:谁关注了谁,有多少关注者,与其他用户共同的关注者。

Imagine a database tables structure like this:想象一下这样的数据库表结构:

table:users     , fields: id, username,num_followers,num_following
table:followers , fields: id, user_id, follower_id , datetime
table:following , fields: id, user_id ,following_id, datetime

Query example to get all followers of the user with id 1:查询示例以获取 id 为 1 的用户的所有关注者:

SELECT user_id from followers where user_id=1;

Query example to get all following of the user with id 1:查询示例以获取 id 为 1 的用户的所有关注:

SELECT user_id from following where user_id=1;

Query example to get all following in common of the user with id 1,2:查询示例以获取 ID 为 1,2 的用户的所有共同点:

SELECT following_id from following where user_id=1 intersect SELECT following_id from following where user_id=2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM