I have two separate tables. I need to SELECT the avatar from the user table, WHERE the username equals from on the comments table. I am trying to create a comment system that displays the user's avatar next to their message.
Comments - ID|Username|From|Timestamp|Message
User - ID|Username|Avatar
$fetchto=mysql_fetch_object(mysql_query("SELECT * FROM user WHERE username='$variable'"));
I think I could display the URL to the avatar using $fetchto->avatar if I had a variable that would pull the avatar of the member making the comment from the user table.
First off your database isn't properly normalized. The comments should refer to the User by UserId, not by Username. Once you've fixed that:
select * from Comments c
join User u on u.ID = c.UserId
Until then:
select * from Comments c
join User u on u.UserName = c.UserName
Also, please stop using the mysql_ family of functions - they're deprecated.
Your query needs to have a simple join, something like this:
SELECT c.*, u.avatar
FROM comments AS c
JOIN user AS u ON c.username = a.username
Related
Lets guess I have 3 tables:
Table a where I have a name and email
Table b where I have a text and user (as a reference to email from table a)
Table c where I have follower and following (both references to email in table a)
Im trying to develop a simple html/php/sql web that allows me to register many users and let them post different texts while also having the chance to follow or be followed by other users (already done) and I want to give an user the possibility to display the texts from table b that he himself posted and those from the users he is following
Im seriously struggling with how to extract this information
SELECT b.text
FROM tableB as b
LEFT JOIN tableC as c
ON b.user = c.follower
WHERE b.user = "currentuser"
This is as far as I got, which only shows the texts posted by the user himself (something I can do way more simple) but I cant seem to understand how to get those from the users he is following
I hope its understandable without any photo
You first want to find all following users in table c rows where the current user is the follower. Then you want to add the current user (or alternatively always have all users follow themselves). Then you want to find all texts for those users.
So:
select b.text
from (
select following as user
from tableC
where follower="current user"
union
select "current user"
) show_users
join tableB as b on b.user=show_users.user
or if you have a tableC row where follower=following for all users, just:
select b.text
from tableC as c
join tableB as b on b.user=c.following
where c.follower="current user"
I am developing a networking site where I have to show random users profiles (excluding members who are already connected) to logged-in user to connect with. I have one members table which contains fields as memberid,firstname and lastname. I have another table for connections which has fields as memberid and friendid.
Now when I use left join on connections table, I get profiles of only connected members which I dont want. I only want to show profiles form members table which are not connected with logged-in user
You can achieve this without Join with the use of Not in.
select *from
members
where
members.memberid not in (select memberid
from connections
)
;
The above query simply means to display info of memberids that are not in connections.
EDIT:
Since you've already used Left join. here is the query to achieve this task with Left join:
select m.*
from members m
left join connections c
on m.id = c.id
where c.id is null;
Hope it helps!
I have two tables in database, named users(store user details) and posts(store post details). Now i want to get data from both tables. Like user_image from users and post description from post.
I am using this query
SELECT * FROM `users`AS u,`posts` WHERE u.user_id IN (SELECT user_id FROM `posts`)
But it returns duplicate data. I have 2 users and 3 posts but it returns 6 posts.
Try something like:
Select a.user_image, b.post_description from users as a join posts as b on a.user_id = b.user_id
Do an inner join & you shall get the desired result
In the above query a & b are alias for the two different tables. I you do not want to use alias you can also write it as users.user_image in your select statement.
Write the fields you want from both the tables in your select statements.
The below image will help you understand the inner join
Inner Join Circle for understanding
Use group by as below:
SELECT * FROM `users`AS u,`posts` WHERE u.user_id IN (SELECT user_id FROM `posts`) group by u.user_id
What about
Select * FROM user u right join posts p on u.id = p.user_id
?
if you want to get data from both tables you need to use joins.and you make sure the two tables are interlinked by primary keys
so use this can help
select user_image,post_description from users join posts on users.user_id=posts.user_id;
I am creating a mini forum for a class assignment, and wish to return the number of comments made on a particular topic according to user ID (the page is a "My Topics" page).
SQL is:
SELECT *
FROM topic
LEFT OUTER JOIN user ON topic.user_id = user.user_id
RIGHT OUTER JOIN comment ON topic.topic_id = comment.topic_id
INNER JOIN avatar ON user.user_avatar = avatar.avatar_id
WHERE user.user_id=1
Where the user ID is returned by the $_SESSION, but for the sake of this question I am setting at 1.
My problem is, the SQL query is only returning topics that HAVE comments. I wish to list all topics regardless of whether comments have been made on them, and also be able to return the number of corresponding rows in the comment table. Any suggestions of where I am going wrong with my SQL would be greatly appreciated!
edit: ignore the avatar stuff, that's just an extra query to display the user's avatar (obviously but thought I should mention that)
Can you try this?
SELECT *
FROM topic
INNER JOIN user ON topic.user_id = user.user_id
INNER JOIN avatar ON user.user_avatar = avatar.avatar_id
LEFT OUTER JOIN comment ON topic.topic_id = comment.topic_id
WHERE user.user_id=1
I have two SQL tables. The first one structure is simple.
ID Name
----------
The second one is simple too.
CommentID Comment CreatorID
----------------------------------
I want to show in my site all the comments that correspond with the "ID of user"
The problem is we have here a lot of ID and two different tables.
Something like this:
$1= $bdd->query("SELECT * FROM comments WHERE id_user=220281");
$2=$1->fetch();
But its impossible because id user is not on the comments table.
The most simple way to do this is to join the tables like this:
select
users.name,
comms.commentID,
comms.comment
from
userTable users
join commentTable comms
on users.ID=comms.ownersID
where
users.id=1
This will return the users name in each row of data, but you don't need to use it in the output more than once.
It also sounds like you could use a few pointers on SQL queries. Do yourself a favour and have a read of this article I put together a while back.
SELECT c.*
FROM comments c
INNER JOIN users u ON c.id_creator = u.id_user AND
u.id_user = 220281
A simple join will do the trick like this :
SELECT c.comment, u.user_name FROM
Users u
JOIN
Comments c ON
c.creator_id = u.user_id
WHERE
u.user_id=220281
fiddle:http://sqlfiddle.com/#!6/3b28a/1