I have three table tbl_user, tbl_posts, tbl_friends
tbl_user stores all the user datas.
tbl_friends stores which user are friends with other.
tbl_posts, where the users posts are stored. userid is the field where all the users id are stored. p_viewstatus field stores the view status of the posts. if the view status of the my friends posts is only by me then that posts should not shown to other users
If sam have a post with id=40 in tbl_posts. The p_viewstatus of the post is only by me then that post must only be visible to sam only.
If sam have another post with id=41 and p_viewstatus!='only by me' this post must visible to all users who are sam's friend
Tried the following query but don't know how to relate the p_viewstatus
Note: I want to fetch all of my posts and my friends posts from **tbl_posts except the p_viewstatus of my friends posts no equal to 'only by me'**
SELECT * FROM `tbl_posts` as p,`tbl_friends` as f WHERE p.`userid`=f.`userid` and p.`userid`=23
Thanks in advance.
BELOW SCREENSHOT SHOWS THE TABLE CONTENTS AFTER JOIN QUERY.
Here's how your query should look like:
SELECT * FROM `tbl_posts` as p
WHERE p.`userid`=23 OR (p.`userid` IN (SELECT `friendsid` `FROM tblfriends` WHERE `userid`=23)
AND p.`p_viewstatus`<>'only by me')
First you select the data from the table you need. After that you filter it with the user id and the ids of the friends of the user and you also tell it that you only want the posts that are not viewable only by me.
Related
I have two tables, one table is for users and stores everything about the user, but more importantly a users profile image stored as a file location. The second table is an images table, where images location and information is stored, such as title and description.
I need to link the profile image to the user who uploaded the photo. The users table has a user_id and profile_image, and the images table has just a user_id.
How can I select all of the images table and just the profile image in one query? Is this even possible?
Edit
I have been looking at joins but can't seem to figure it out. The tables look like this:
IMAGES USERS
id user_id
user_id username
username password
title isadmin
image (location) points
description signup_date
points email
category city
bio
profile_image
I need profile_image to be selected so it can be called upon using $row[profile_image], but I also need all of the information from the images table. Hopefully this clarifies things!
You can use a join based on user_id
eg for select all the columns for the user_id = 1
select USERS.* , IMAGES.*
from USERS
INNER JOIN IMAGES ON USERS.user_id = IMAGES.user_id
WHERE USERS.user_id = 1
I working on a secured blog where I have a specific post displayed in separate page once a user clicks read more button. This separate page includes registered users comments.The issue I have is that the name of the user who wrote the comment is not matching the actual user, its showing instead a name of a user who wrote a post. My sql query:
$comsql="SELECT c.*,u.name FROM posts p JOIN users u ON p.user_id=u.id"
." JOIN comments c on p.id=c.post_id ORDER BY commentdate DESC";
My echo:] Written by: , On: ">Edit comment | ">Delete comment
Yeah, it's what you ask:
ON p.user_id=u.id
You should have somthing like this:
ON c.user_id=u.id
In your SQL query you're matching the user of the post instead of the user of the comment. Do you have a user_id field in the comments table ?
Probably, both comments and users tables have name field. To fix this;
Change query to:
$comsql="SELECT c.*,u.name AS username FROM posts p JOIN users u ON p.user_id=u.id"
." JOIN comments c on p.id=c.post_id ORDER BY commentdate DESC";
If doesn't work, please leave a comment
DB Schema is given below. how can i fetch posts plus shared posts of users like Facebook. in Facebook wall the we can see original posts and the posts user shared. how can i actively get the posts. records could be in millions.
i have tried using joining two tables but i am unable to identify the shared posts.
SELECT *
FROM
post_shares
INNER JOIN posts
ON (post_shares.post_id = posts.id);
select id, description from post where user_id = 'xyz'
union
select id, description from post p, post_shares ps where p.id = ps.post_id and ps.user_id = 'xyz'
Top part of the query will give you posts for user 'xyz' and second part will give you posts that the user shared.
I`m working on a small website where people can follow each other. There are newsfeed on the site where users can see updates from other users that they follow. The problem is that I can see only posts from other users and not my posts. I want the current user to be able to see his posts too Ordered within the other users posts. Something like Facebook Wall.
I have tried tons of differend queries and php, but I get only other users post or when i manage to pull out my posts too there are dublicate post for every user that follows me.
I have 3 tables 'members', 'Post' and 'follow'. The members table hold UserID, Name and Last Name. Post table holds the UserID(FromID in the table) Indexed to members UserID and the post from the user. In the follow table there are 3 columns FollowID, Followed and Follower both Followed and Follower are index to members UserID.
At the moment I use that query:
$query= "SELECT Post.*, members.*, follow.* FROM Post
INNER JOIN members ON Post.FromID=members.UserID
LEFT JOIN follow ON members.UserID=follow.Followed
WHERE Post.FromID='$user' OR follow.FollowerID='$user'
ORDER BY Post.date DESC, Post.time DESC";
$user is the UserID of the current logged in user.
That query returns all the posts that I want but the problem is that my post are show /n times for each user that follows me.
I really would appreciate the kindness to the one that give me some directions what i do wrong. Thank you
The problem is with your $user. Currently what is happening is that you have an OR condition in where clause. And you are trying to pull either your data or ur followers data.
My solution for you would be to remove $user from the query you have. So you will have list of followers data only. Have a different query for your posts and then while displaying sort them by datetime Timestamp so you have your posts with other followers too.
To have your posts and folloeers posts in one query.
Use union. Like :
(Query of your posts ORDER BY date)
UNION
(Query of followers posts ORDER BY date);
Also check UNION usage in w3schools.
Hope this helps
I think this query might be easier to understand:
$query = "SELECT * FROM Post,members,follow
WHERE
Post.FromID=members.UserID
AND
members.UserID=follow.Followed
AND ( Post.FromID='$user' OR follow.FollowerID='$user')
ORDER BY Post.date DESC, Post.time DESC";
Okay, so I want to have a news feed on my website. I have 3 tables named Users, Follow, and Posts. Basic user data goes into the Users table, who is following who is stored in the Follow table, and whatever the user posts goes into Posts. Now, I know how to select every post from a database table and limit it using the WHERE clause. But how would I write my query to select all all of the posts from only user's they are following, and display them by DESC date? Thanks.
Here's a general layout for you, make three tables like you mentioned, I've outlined below just as an example
[TABLES]
users
followers
posts
In the users table you should have at least columns like userid (auto incremented value / primary key).
The followers table should have something like userid which would match to the users table userid, a followid column which would also have the id # for the follower from the users table.
Then for your posts table you would want to have userid too so when each post is made it would have the id from users table.
Then you would need to do something like:
SELECT p.*
FROM posts AS p
WHERE p.userid IN (SELECT followid FROM followers WHERE userid = ###)
ORDER BY p.date DESC
Now it really depends on how you are getting the users id to figure this out. If your passing the users id using a session after they logged in similar to something like Facebook, then you could change userid = ### to something like userid = ".$_SESSION['userid']." But again it really depends on how you pass the users id but the above should at least get you somewhat started.
Make sure to put indexes on the userid, followid columns so that when the table becomes larger it will do the joins quickly.
An alternative to #Shane's answer is to use the JOIN operator:
'SELECT p.* // I prefer to name all the fields, but for brevity's sake I'll use the shorthand
FROM Posts AS p
INNER JOIN Follow AS f ON p.userid = f.followid
WHERE f.userid = '.$selectedUserID.'
ORDER BY p.date DESC;'
For an inputed User ID ($selectedUserID), it will find all User ID's of the people they follow (matching follow ID to user ID on the Follow x-ref table) and then find their respective posts from the Post table in descending order by date. It will return empty if they do not follow anyone or the people they follow have no posts.
I also hope I do not need to remind you to sanitize your input to the database and escape your output to the web.
Is this what you're looking for?