Database structure for "follow" feature similar to twitter? - php

I want to create a similar feature as Twitters "follow" feature but wondering a bit about how you would create a database structure for something like that. Say we have a user table like this:
id, username
And then a posts table that looks like this:
id, userid, post, date
What i want to do is to let a user follow other users, and then in a stream get their posts. So i'm guessing there should be a follow table. Where you can insert which user follows which user. But then how do i query a stream out of this? If user 1 follows user 2 and 3. I want to make a query like
SELECT posts FROM $peopleIfollow ORDER BY date
But as you see the above example is not a valid query, how can i gather "follow"-information and query the results? Or more what is the structure i should be looking into to achieve this? Should followers be put in an array and is it possible to have an array in a query?

Quick and simple solution that I can think of...
following(follower_id, following_id)
Then you'd get posts something like
SELECT *
FROM post_table
LEFT JOIN following
ON following.following_id = post_table.userid
WHERE following.follower_id = ?
LIMIT 0, 20;
Where ? is the ID of the current user. This would then join the tables and pull the posts of the person that the follower is following. You could also throw in an order by date.
This SQL statement could be vastly improved, but that's a quick and simple idea for ya.

Related

Error trying to Select columns from different tables in MySql

I can't figure whether to use JOIN or UNION when trying to set this up, but when I found this answer: MySQL Select all columns from one table and some from another table I thought this might work. Turns out I still get an error. Is there something I am doing wrong with setup of this?
$sql = "SELECT user_images.*, users.profile";
Basically I have user images (user_images) in one table and I also want to display some user information such as the profile column in users table.
If you get an error please include it in your question. It's relevant information and you know we're going to ask what it was.
But this is a sample query for what you want to do.
SELECT user_images.*, users.profile FROM user_images JOIN users ON user_images.id = users.id;
Joins need a linking column to work correctly, something that is the same for both rows in both tables. I've used id as an example but it may not be correct for your specific situation.

PHP/MySQL filtered search similar to Facebook

I'm trying to build a simple custom search for a Wordpress site using PHP and a mysql db that will return results from many different tables such as Groups, Users and Sites where none of the data per table relates to another table being searched. I'm aiming to build this similar to Facebook where it groups results based on the type of result.
I have the individual queries built but I am stuck on how to tie them all in together so that I can filter through the types of results and group them accordingly like Facebook does. Is there an easy way to tell which table results came from or would that be heading in the wrong direction entirely? I don't know enough about mysql to know best practices. Here is a sample query that I am running:
SELECT
name, slug
FROM
".$bp->groups->table_name."
WHERE
name LIKE '%".$search_string."%'
OR
name = '%".$exp_string[0]."%'
OR
name IN ('".$join_string."');
Try this:
SELECT
name, slug, '$bp->groups->table_name' as table
FROM
".$bp->groups->table_name."
WHERE
name LIKE '%".$search_string."%'
OR
name = '%".$exp_string[0]."%'
OR
name IN ('".$join_string."');
This should output the table name into the result. I don't know how you may want to group it afterward..

PHP MySQL database design

I have a user table which has username,user_id,password and email fields.
The user can post updates,can subscribe to users and can friend people.
What is the best way to design my database in order to store the count of the updates, subcribers and friends of a user?
Should i add 3 more fields in the user table and just increase the count of these 3 columns?
Thanks.
You can make 3 tables: friends (user1_id,user2_id), updates(user_id,content),subscribe(user1_id,user2_id).
If the database is big and you don't want to count the friends each time, you can add a field in users table and update it every time a friend is added/deleted
Maybe drawing a diagram would help:
http://en.wikipedia.org/wiki/Entity-relationship_model
First I'd try to calculate
the count of the updates, subcribers and friends of a user
on the fly, something like
select count(*) from user_updates where user_id = :uid
select count(*) from user_subcribers where user_id = :uid
select count(*) from user_friends where user_id = :uid
If you have performance problems with this approach, then you can consider adding these count fields to user table, but you have to be careful to maintain them.
Not sure if i understand your question but i think the best way to do this is to make a table for each of this functions, you can link those tables with the user_id field. Like so:
Table 'Users' :
*user_id*
username
password
email
Table 'Friends':
id
*user_id*
friendids
You can then get the users with something like
SELECT * FROM friends WHERE user_id = '$userid'
Either create a separate table with that user meta data or add them as new columns. Either way is fine.
I'll assume you meant to type "columns" instead of "friends" in that last sentence.
That's one way to do it.
You should think about the entities, relationships, and cardinalities when you design. Do you want to keep track of timestamp for each update? If yes, I'd make that a separate table with a one-to-many relationship to user. Same for subscribers and friends. The answer depends on how you choose to model the problem. There's no right or wrong answer, but there are tradeoffs.
Well, it is faster to read if you have separated relations and content, however if you have only content and generates relations from it, the result sometimes could be more accurate. So, it depends.

How to search multiple tables in MySQL database for exact match using PHP

Here's the table structure
table : user
id
name
email
category
table 2 : body
id
uid
height
haircolor
Here's how i access the data from the database
SELECT * FROM user WHERE category='paid' and with more codes it works.
What i want to do is like this (i..e allow complex search)
Select * FROM user WHERE category='paid' body.height='5ft', body.haircolor='red' WHERE user.id=body.uid
I know the statement is wrong, but i want the database to be searchable such that i can select haircolor as red, height as 5ft and the script should only return user(s) whose height is 5ft and haircolor is red (exact match)
I hope you guys understand my question.
P.S : As you can see i have used 2 tables, 1 to store user info and 2 to store user body info. I can integrate them into 1 but i want to keep it as it is.
It looks like you have a bit of confusion based on using a WHERE for both the conditions of the select, as well as specifying the connection between your two tables. This is why I prefer to keep the joins explicitly defined, which would give you a query like this:
SELECT user.*
FROM user
JOIN body ON body.uid = user.id
WHERE body.height = '5ft'
AND body.haircolor = 'red';
So now you've separated the part that connects the two tables out into the JOIN/ON section, and the WHERE only includes the conditions that narrow down the search.
You can actually do something very similar to what you're doing above - you just use multiple tables in your FROM construct. It's a great way when you want to pull something out quickly.
SELECT user.* FROM user, body
WHERE user.category = 'paid'
AND body.height = '5ft'
AND body.haircolor = 'red'
AND user.id = body.uid
Remember to specify the SELECT in this instance - everything from the user table. You could also do this with a JOIN, although I have a phobia of them so tend to use the previous method.
SELECT * FROM user
WHERE user.category = 'paid'
AND body.height = '5ft'
AND body.haircolor = 'red'
JOIN body ON user.id = body.uid
That should do the trick! As I said, I despise using JOINs, and the former should be absolutely fine.

Most Efficient Way to Get a Query of Posts based upon if a User Follows Them

Right now I have two tables for getting a timeline of posts that a user follows in MySQL, timeline and follows.
In the 'follows' table, there is the 'follows.follow_From' column, the 'follows.follow_To' columns to show where the follows are from.
In the timeline table, there is a 'timeline.post_From' column containing the author's user ID of the post.
What is the most efficient method of selecting all posts from 'timeline' only where the current user has a "follow" in the 'follows' table? I am currently using a MySQL array, but I don't see it as efficient. Thank you for reading this and your help!
I'm not an expert and it might not be the most efficient but I'd guess:
SELECT follow_To FROM follows WHERE follow_From = 'user-id'
SELECT * FROM timeline WHERE timeline.post_From = (the results above)
e.g. SELECT * FROM timeline WHERE timeline.post_From IN (SELECT follow_To FROM follows WHERE follow_From = 'user-id')
Doing the other way round would definitely be slower because your processing more data then cutting it down. Cutting the data down first then look up is better.

Categories