I am creating a social website.I have an alert option which needs to show the friend requests and unread messages together based on the time it arrives in various div's .Actually I am lack of logic about how to display results from two tables.Kindly help me.
My tables are:
1)sk_friends=>[friendship_id, from_user_id, to_user_id, status,date]
status contains accepted or pending as values.
2)sk_messages=>[msg_id, from_user_id, to_user_id,text, date, status]
SELECT * FROM sk_friends JOIN sk_message ON sk_friends.from_user_id = sk_message.from_user_id
this will be the basic query for retrieving records from two tables..
try this.. but not sure how much will help you
If you're trying to pull both pending friend requests and messages at the same time, you'll probably want to consider adding a more generic "notifications" style table, or run them as separate queries.
So you could go for a table along the lines of:
sk_notifications => [id, date, type, from_user, to_user, status]
Which would contain both types of events (new messages and new friend requests) - your code would then be responsible for providing the different actions based on the notification type.
for friend request see the query below
select * from sk_friends where (logged_in_user_id in from_user_id or logged_in_user_id in to_user_id) and status='pending';
and for message unread
select * from sk_messages where to_user_id=logged_in_user_id;
Related
I have a screen that looks very much like facebook timeline
users can view posts of other users etc.
to get these posts i do something like
select user.id,user.name,posts.title,posts.body from posts left join users;
now data i need to collect is "Who saw this post" .
is there any elegant way to do it ?
right now all what i can think of is every time i fetch posts. i loop over them, then collect all ids of posts that the query returned and then push in another table
user_views [pk:user_id+postId]
userId,postId
1 , 1
Then when i'm fetching posts next time i can do count of user_views.
select *,count(user_views.id) from posts join user_views on post_id = post.id
but this sound like a lot of work for each VIEW, specially that most probably user will see a most multiple times,
is there any known patterns for such need ?
This is a design question and the answer really depends on your needs.
If you want to know exactly who viewed what post and how many times, then you need to collect the data on user - post level.
However, you may decide that you do not really care who viewed which post how many times, you just want to know how many times a post was viewed. In this case you may only have a table with post id and view count fields and you just increment the view count every time a post is being viewed.
Obviously, you can apply a mixed approach and have a detailed user - post table (perhaps even with timestamp) and have an aggregate table with post id and view count fields. The detailed table can be used to analyse your user's behaviour in a greater detail, or present them a track of their own activities, while your aggretage table can be used to quickly fetch overall view counts for a post. The aggregate table can be updated by a trigger.
Just a question about what the best way is to send a message to a group (Like whatsapp group messaging).
I save the members per group into a separate database table and once a user sends a message to the group it retrieves the users from this table. I'm a bit stuck on part two:
As I see it there would be three methods.
Insert the message into a database for the entire group. Send the message back to the different users based on groups they are member of.
Insert the message in the database per user of the group. So loop the members and insert for every member. This has the advantage over the first method that downloaded, read and deleted status can be tracked.
Create a table for messages and one for the message-recipient link. But I'm not really sure how a would query in the most optimum way as to retrieve all the data (downloaded, read, etc) for a group of users. Is that possible in a single query?
Would anyone know what the best method would be? I can imagine that method 2 will fill up a database pretty fast, but method 1 doesn't have the ability of tracking status for a message.
At the moment I am using method 1, but I run into the problem that when a user deletes a message. It would still be returned on other devices because there would be no easy way of setting a certain deleted flag for group messages.
Does anyone know how apps like telegram, whatsapp and so on do this? And would method 2 give problems later on when millions of messages are sent?
Regards,
Ron
I would seperate it like this.
Messages
id, message, owner_id, deleted
Groups
id, name
Message_Group
message_id, group_id
User_Group
user_id, group_id
Then you can don't need to add a message to all users in that group. When you insert a new message you first inserts it into Messages, catch the id and inserts it into Message_Group with the message id and group id. To get the messages of a group just join Message_Group and Messages and you are pretty much done. Of course you also needs to join the user table to catch who wrote that messages.
I'm building a private social network with Yii that will have "comments" all over the site - in Profiles, Events pages, Group Threads, etc. When a user makes a post, they will be able to select the visibility of that content as:
Anyone
Registered Users Only
Friends Only
Custom (specific list of friends)
I'm trying to figure out how to model this for speed. I've considered using MySQL for writing the setting into a binary "is_secure" field in the Comments table - if it is true, then go to a table with three columns: comment_id, user_id, and group_id. Groups (group_id) would be for groups of users - Registered Users, Friends. Custom would make one row for each user that is selected (user_id).
This table will get huge (perhaps several dozen rows for each comment), so I'm wondering if using NoSQL is worth considering here for retrieval only, or if there's a better way to model this.
Thanks so much!
Similar question to database "flags". Search for related SO questions.
Instead of an IF true/false with the is_secure field, just add 1-bit fields for read_all (anyone), registered, friends, custom. Add another table which holds the custom list would have comment_id (from the previous table) and friend_id (multiple rows). That way, in a single query with a LEFT JOIN on custom_friends_list_for_comments you can determine whether or not to show the page to a user. Optionally, custom could be a comma separated list (char field) but size limits might be an issue. Assuming 3-letter friend ids with a comma, each 255 char field can have 64 friends.
I am trying get the last three posts and statuses, as I know the status table contains only the status that I put on my Facebook page. The posts table contains a shared link, posts made by me or other users, so I want to: compare the last three statuses with the last three posts and take only the most 3 new (created date) from them, so I need to make check the created date or updated date for both sides and the output should be three.
I have tried:
Take the last three posts and the last three statuses, compare the updated date with the most updated three, put them in an array. That took a lot of PHP code. Can I do that with FQL?
You can get the last three posts and statuses via Graph API. Use:
https://graph.facebook.com/fb_id?fields=id,name,statuses.limit(3).fields(message),posts.fields(message,caption,description)
For more help, https://developers.facebook.com/tools/explorer
FQL
For getting statuses
SELECT status_id, message FROM status WHERE uid=fb_id
For getting posts
SELECT post_id, actor_id, target_id, message FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed') AND is_hidden = 0
But for all these you should have the permissions for accessing them. See
http://developers.facebook.com/docs/reference/fql/stream/.
this is a simple question.
With PHP and MySQL, I'm trying to build a bug reporting application.
Currently, the user can post bugs and a list of bugs is portrayed to the technician/administrator.
How do I make it so that many technician or administrators can reply to the report (thread?)
As in both mysql table layout and PHP coding?
The current MySQL table has layout of:
id (Bug ID)
by (person reporting it)
title (Report Title)
content (Contents of Report)
datetime (Date/Time of report)
application (Application/page where problems happens)
priority (Priority)
assigned (Assigned To)
status (Status of report/bug)
So no response column yet, but how do I achieve multiple post/responses with PHP and MySQLi?
Thanks
This would be a many-to-one relationship. You can either have:
response table
id (response id)
bugid (bug id)
columns related to the response
or
response table
id (response id)
columns related to the response
with
bugresponse table
responseid (response id)
bugid (bug id)
columns related to the bug-response relationship
where the second design can also handle many-to-many relationship (unlikely to be necessary in this case) and can also has some other benefits depending on your requirements.
You make another table with the responses. For instance with the layout
id (Response Id),
responseTo (id of the Bug this is a response to),
by (person responding),
content (Contents of Response)
Where responeTo is the crucial field. Then when you want to view all the response to a bug you just select from the response table where responseTo = currentBugId.
What you usually do is you create a separate table for the responses. In that table you have one field that "points" to the first table. It could look like this:
TABLE responses
id (Unique id)
bug_id ("Pointer" to which bug this response "belongs to")
body (Contents of response)
This way you can have many responses which all point back to one bug, and thus you have virtually created an "ownership" or "relation". It is customary to call a relation like the one above a "one to many" if we pretend we're in the bugs table, looking out. (And a "many to one" of we're in the responses table, looking back at the bugs table.)
Then, in PHP, when you want to retrieve all the responses belonging to a bug, you do something like this: (pseudo code)
$bug = SELECT * FROM bugs WHERE id = $some_id
$resps = SELECT * FROM responses WHERE bug_id = $bug['id'] ORDER BY created_at
Voilá! Now you have the bug and all of its responses, ordered by creation date. When you insert new responses you need to set bug_id to the appropriate value, of course.
Cheers!