Who can help me?
I have 4 tables posts, users, users details and comments.
Here is the strucutre:
posts
id
title
description
users
id
username
password
users_details
id
user_id
avatar
location
comments
id
post_id
user_id
parent
comment
posted
I have a query which i want to display comments on specific post.
This is what i written but avatar column it doesn't show any results. It seems i written it wrong on join or something.
SELECT comments.id as comment_id,
comments.user_id as user_id,
comments.post_id as post_id,
comments.parent as parent,
comments.comment as comment,
comments.posted as posted,
users_details.avatar as avatar,
users.username as username
FROM `comments`
LEFT JOIN users ON users.id = comments.user_id
LEFT JOIN users_details ON users_details.user_id = users.id
WHERE comments.post_id='60337'
ORDER BY posted ASC
Here is a screenshot of the table with results https://i.imgur.com/VQ8KwV1.png
I want to mention the avatar field it has a value for that user in users_details table so it should be displayed...
Thanks!
Two potential reasons I could tell causing this issue.
1. The users do not have an avatar.
Try: Select Avatar from Users_Details where User_Id in (2,9212) and make sure it returns some data.
2. The Primary/Foreign key being used for joining the tables is not matching
Run those queries below and ensure that they are returning the same users.
select * from users where id in (2,9212)
select * from users_details where user_id in (2,2912)
Related
Newbie here.. Here's what I'm trying to do.
I have a Posts table with the following columns
id, userid, bodyText, date
and I have another table for users users.
When users submit a post, the user-id saves into the "posts" table under "userid". Well, I want to display that users information, such as name and picture, on the posts using the "userid" to get each users information so that their name and picture shows along with the post they submitted.
Sorry if I'm not being clear, english is not my first language and like I said, I'm new at this and still trying to learn.
I had it where when they submit the post, their name and picture also saves into the "posts" table but I want to change it because if the user updates their name or picture, it will still show the name and picture they had when they submitted the post. I want their name and picture to update on the posts if they update their information on the users table.
What you are asking about is a JOIN. In general the Join will look like
SELECT p.id. p.bodyText, p.date, u.name, u.picture FROM posts as p INNER JOIN users as u ON p.user_id = u.id where p.id = 123;
This will select the post with the ID 123 from the posts table and joins the users information from the users table based on the user_id columns value from the posts table.
In Syntax this could be something like
$sth = $dbh->prepare("SELECT p.id. p.bodyText, p.date, u.name, u.picture FROM posts as p INNER JOIN users as u ON p.user_id = u.id where p.id = ?");
$sth->execute(array(123));
$red = $sth->fetchAll();
I am trying to display the profile name of a user's friend. I have a friends table (users table) and the myfriends table (friend table). Don't ask why the stupidly confusing names, it is what the assignment requires. Ill refer as the friendsID as user_id and myfriendsID as friends_ID just to eliminate confusion.
Now, so far I have got it to list the user_id and the friends associated with that ID.
I want it to be able to also display the friends names through a profile_name I have on the user_id table.
The SQL i have at the moment is:
SELECT friends.friend_id, myfriends.friend_id2, friends.profile_name
FROM friends
INNER JOIN myfriends
ON friends.friend_id = myfriends.friend_id1
WHERE friends.friend_id = '2'
Problem is, is that it lists the name of the USER_ID, not the friend_id. The profile names I am trying to obtain are also from the user_id table, but with their own unique user_id. If that makes sense?
Join back to friends/users again.
Inner join friends as f2
On f2.friend_id = myfriends.friend_id1
There are three tables
posts table
id username
1 ahmed
users table
id title
1 test post
post_users
user_id post_id
1 1
post_users (have two fields user_id and post_id) many to many
when I want to send post id=1, I want to select users from users table that don't have a row in post_users table (in one query). How?
I think what you are looking for is something along these lines. INNER JOIN:
SELECT DISTINCT USERS.ID
FROM USERS
INNER JOIN POST_USERS
ON USERS.ID=POST_USERS.ID
WHERE POST_USERS.POST_ID <> your_input_value
I made a few assumptions, if this doesn't work please provide some more details.
I am working on a project right now and am rather stumped with a specific sql query I (need) to execute. Let me start off by showing the DB structure I need to pull from.
--posts_table--
ID
post_title
post_text
bus_id
This next table is what is screwing with me. The only way data related to the logged in user is in here is if they have "liked" a specific post -- otherwise there is no data related to that user in this table. Now there could be plenty of data related to a particular post, just generated from other users.
--likes_table--
ID
user_id
post_id
like
What I need this to do is grab all the posts from the post_table above where a specific business id is specified. From there, I need it to grab the "like" column in the likes_table if there is data in there related to the logged in user. If there is no data there, just leave that field null in the query. Below is a query I wrote that works until there is other "like" data in the like_table from other users.
SELECT posts.id, posts.post_text, posts.post_title, likes.post_id, likes.like
FROM posts LEFT JOIN likes ON posts.id = likes.post_id WHERE
posts.bus_id = 1 AND likes.user_id IS NULL OR likes.user_id = 1;
This works up until data has been entered in the table about a specific post being liked by a different user before that user has done anything with that post, whether they like or dislike it. I am not sure if this specific type of query is even possible, any help would be much appreciated.
Edit:
After looking at it again -- I got it, finally. I just needed to add one more AND. Below is the proper query I was looking for.
SELECT posts.id, posts.post_text, posts.post_title, likes.post_id, likes.like
FROM posts LEFT JOIN likes ON posts.id = likes.post_id AND posts.user_id = 1 WHERE
posts.bus_id = 1 AND likes.user_id IS NULL OR likes.user_id = 1;
Ahh, I think I get you -- is it that if a particular post hasn't been commented on by user_id number 1 at all, the row for that doesn't show up at all?
In that case, put your l.user_id=1 into the JOIN condition instead of the WHERE condition --- this will put a NULL in if user_id 1 hasn't liked or disliked a particular post.
SELECT p.id, p.post_text, p.post_title, l.post_id, l.likes
FROM posts p
LEFT JOIN likes l ON p.id = l.post_id AND l.user_id=1
WHERE p.bus_id = 1
The l.user_id IS NULL OR l.user_id=1 has been incorporated into the LEFT JOIN -- it doesn't make rows for the other user_ids.
I need to build an activity feed to go on each users profile page showing what they have been doing on the site.
There is three tables: comments, ratings, users
I want the feed to include the comments and ratings that the user has posted.
in the comments and ratings table it stores the user id of the user who posted it, not the username, so in for each item in the news feed it needs to select from the users table where the user id is the same to retrieve the username.
All the entries in the feed should be ordered by date.
Here is what ive got even though i know it is not correct because it is trying to match both with the same row in the users table.
SELECT comments.date, comments.url AS comment_url, comments.user_id, ratings.date, ratings.url AS rating_url, ratings.user_id, users.id, users.username
FROM comments, ratings, users
WHERE comments.user_id=%s
AND comments.user_id=users.id
AND ratings.user_id=%s
AND ratings.user_id=users.id
ORDER BY ratings.date, comments.date DESC
JOIN. It seems you know that, but here's how:
SELECT * FROM comments LEFT JOIN users ON comments.user_id = users.id
Thus, as far as I can tell, you're trying to order two separate things at the same time. The closest I think I can come up with would be something like:
(SELECT comments.date AS date, users.username AS name, comments.url AS url CONCAT('Something happened: ',comments.url) AS text
FROM comments LEFT JOIN users ON comments.user_id = users.id
WHERE users.id = %s)
UNION
(SELECT ratings.date AS date, users.username AS name, ratings.url AS url CONCAT('Something happened: ',ratings.url) AS text
FROM comments LEFT JOIN users ON comments.user_id = users.id
WHERE users.id = %s)
ORDER BY date DESC LIMIT 0,10
Note that the columns of both parts of the union match up. I'm pretty sure that that is required for something like this to work. That's why I have that CONCAT statement, which lets you build a string that works differently between ratings and comments.