简体   繁体   中英

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. 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

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. 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)

I think a good way is using mysql tables where fields are related by 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:

SELECT user_id from followers where user_id=1;

Query example to get all following of the user with 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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