How to fetch results from user,post,comments tables in one query - mysql

The title may not be the fit to the question perfectly ! pardon me
Goal
I want to achieve the name of the user who has done a post,the description of that post(p_description), the comments that are there on that post,and the name of those who has provided comments (comment_description)
How can I achieve that ?
I've three tables:
user
post and
comments
User table is like :
u_id name and so...
Post table, here it is :
u_id p_id p_description
u_id as foreign key from user table ,
and the Comments table is:
u_id p_id comment_description.
note that the u_id and p_id in table comments are both as foreign keys from table user and post respectively.
I'm writing the following query for the above Goal:
SELECT p_description,f_name,comment_description as COMMENT FROM user JOIN post ON user.u_id = post.u_id JOIN comments ON user.u_id = comments.u_id
It gives me the post description, name of that guy who has done post, and the comments on that post, and I want to have also the name of those guys who has done comments to that post.
If the problem is not explained well.
simply I want query for the above GOAL regarding those three tables.

Join user once more on the user ID in comments.
SELECT u1.name,
p1.p_description,
u2.name,
c1.comment_description
FROM user u1
INNER JOIN post p1
ON u1.u_id = p1.u_id
INNER JOIN comments c1
ON c1.p_id = p1.p_id
INNER JOIN user u2
ON u2.p_id = c1.p_id;

Related

phpmyadmin query using multiple tables

Ill make it simple I have 2 tables in my database
1. Post
2. User
The columns inside the Post table are
Post ID
User ID
Post Title
Post Content
Then the columns in User table are
User ID - Connected to Post.User ID
User Full Name
Then I want to do now is to select the post posted by Juan Cruz(User.User Full Name). Help Please, I cannot find these question anywhere.
PS Sorry for my bad english.
EDIT: I will use this as my search module on my php/html project. So I need to know the post/s posted by Juan Cruz insted of User ID of Juan Cruz. Thanks Again
Hope this might help:
SELECT
p.post_id, p.user_id, p.post_title, p.post_content,
u.user_fullname
// column name from two or more tables
// eg: table_name.column_name
// use p and u, because we already give temporary name for table post and user
FROM
post p // table 1: post refer as p (SQL aliases)
JOIN // combine 2 column from two or more tables
user u // table 2: user refer as u (SQL aliases)
ON
p.user_id = u.user_id // key for joining the table, the ID should be the same
or you can use below code without aliases :
SELECT
post.post_id, post.user_id, post.post_title, post.post_content,
user.user_fullname
FROM
post
JOIN
user
ON
post.user_id = user.user_id
Refer: SQL JOIN
You need to use a JOIN. This should be updated to use your column names.
SELECT p.id, p.title, p.content, u.full_name FROM User AS u JOIN Post AS p ON (u.id = p.user_id);

LEFT JOIN in MySQL query

I have two database tables, one is named "articles", and the other one is "users"
the structure of articles is as follows:
article_id article_title user_id
The structure of users is:
user_id user_fullname password
I wish to retrieve a list of all articles from the table "articles", but would like to attach each article's user_fullname. I think this may require "LEFT JOIN", so I made the following attempt in MySQL prompt.
> SELECT * FROM articles A LEFT JOIN users U on U.user_id = A.user_id;
but somehow I don't see the user_fullname printed out with this command. I need some help with the correct syntax. Thank you!
A full join seems more appropriate, each article must have an author, so there should be a corresponding entry in the users table:
SELECT *
FROM articles a
JOIN users u USING(user_id);
Note: USING here is the same as ON a.user_id = b.user_id and can only be used if the column name is the same in both tables.

Joining on the same table more than once

I'm probably being a bit dumb, hopefully someone can help.
I have a simple 2 column user table (ID, USERNAME).
I have a comments table for images (COMMENT, COMMENTFROM, COMMENTTO)
COMMENTFROM is the ID of the user who made the comment. COMMENTTO is the ID of the owner of the image that the comment was added to. Both users are held within the USERS table.
I want to pull out and display rows like this
"really nice photo" - to USERXYZ - from USER123**
This has puzzled me, because if I join the USERS table to the comments table on:
WHERE comments.userfrom = users.id
That only gets me one (or the other) of the 2 usernames I need per row. Is there a way I can get both?
I'm not even sure how I would search for this answer on SOF, apologies if it has been answered before. If anyone can point me in the right direction it would be appreciated :)
You need to JOIN to the users table twice, and give them different identifiers (aka aliases) on each JOIN within your SQL.
SELECT
comment,
userFrom.username AS commentFrom,
userTo.username AS commentTo
FROM comments
JOIN users AS userFrom ON userFrom.ID = comment.commentFrom
JOIN users AS userTo ON userTo.ID = comment.commentTo
Please try following one
SELECT CONCAT(c.comment," - to ",
(SELECT USERNAME FROM user WHERE user.ID = c.COMMENTTO LIMIT 1),
" - from ",
(SELECT USERNAME FROM user WHERE user.ID = c.COMMENTFROM LIMIT 1)) FROM comments c

PHP/MySQL Many to Many. How?

I recently thought that storing an array in MySQL database would solve my problems... then I went and banged my head on a wall until I realised that there was an easier solution to my problem. Unfortunately I can't seem to figure out what the hell I need to do.
I have two tables. User and Entry. Each user can be the Author of any number of entries. One to many right? Well the problem comes from the fact that each entry can have many users. -_-;
As you can imagine, doing this using arrays would be...not only cumbersome but also stupid, especially if someone managed to accumulate 2000 entries. I had anticipated that I might need to create an extra table and some resources do seem to suggest it but the resources aren't as clear as I would have hoped. I will need to get all the users for a post and somewhere else, I'll need to get all the posts for a user.
Can someone explain the best way of doing this? (Without using arrays of course =p)
you need to make 3 tables:
user (primary key = id)
id
name
...
entry (primary key = id)
id
text
...
userentry (primary key = complex key user,entry)
user
entry
if you want to get an entrys users, just do
SELECT
user.*
FROM
userentry
INNER JOIN
user
ON
userentry.user = user.id
WHERE
userentry.entry = [your entry-id]
and to get all entrys of a user, just do it the other way around:
SELECT
entry.*
FROM
userentry
INNER JOIN
entry
ON
userentry.entry = entry.id
WHERE
userentry.user = [your user-id]
The best way to do this is to have three tables:
Users:
Id,
Name,
...
Posts:
Id,
PostName,
...
UserPosts:
Id surrogate key,
UserId,
PostId.
Then to get all the users for a post
SELECT u.*
FROM users u
INNER JOIN posts p ON u.id = p.userid
WHERE p.id = apostid
To get all the posts for a user:
SELECT p.*
FROM posts p
INNER JOIN users u ON u.id = p.userid
WHERE u.id = auserid

Join on a cyclic relation (beginner)

I'm a beginner in queries and I'm struggling with one of them. Here are the two tables involved :
The askstobefriends table permit a user to add a friend in the application I m developping. The relational form of it is :
AskToBeFriends(ID (long), #UserAsker (long), #UserAsked (long), Accept (tinyInt))
So with this table we can see who asked to be friend and if it was accepted ...
The query I m trying to realize would permit to list all the user's friends from his ID and also return the friendship statut (accept field ==> waiting for an answer, accepted or refused).
Speretaly, it would be something like that :
SELECT Accept, UserAsker, UserAsked
FROM askstobefriends
WHERE UserAsker = '".$userID."' OR UserAsked = '".$userID."' ";
==> first issue : it can either be the user who asked to be friend with someone or the opposit, that why i've put and OR. After that, I d like that for everyfriend founded there's these informations :
SELECT colUserID, colUserLogin, colUserName, colUserFirstname
FROM userTable
WHERE colUserID == FRIEND
So I guess I need to do a join query, and in my join I have to be sure that I'm using the right foreign key from the asktobefriends tablefor each cases !! (once the key could be UserAsked and another time UserAsker depending on who asked to be friends :S )
Does anyone have a clue please :S ?? Thanks ;-) !!
Your design is wrong. A User asks to be friend of another User, so "Ask_to_be_friend" is the relation, and the cardinality is many to many, so the design will looks like this:
User_User_ID is UserAsker.
User_USer_ID1 is UserAskedtobefriend
and the query could be like (you'll get all the users that user_user_ID Asks to be friend of):
Select U.* from User as U
Join Ask_to_be_friend as A on
U.user_ID = A.User_user_ID
--where a.accept=1 if you add this, this will give
--you all the friends ID of the user_ID table User
If you want to get the names or extra info of the askedtobefriend you'll need a extra Join
Select U.* from User as U
Join Ask_to_be_friend as A on
U.user_ID = A.User_user_ID
Join User as U2 on
A.User_User_ID1=u2.User_ID
--where a.accept=1 ,with this you'll with get only the friends
You could join the tables using criteria that ensure only friends of :userID are returned. For example:
SELECT u.*, a.Accept
FROM askstobefriends a JOIN userTable u ON (:userID, u.colUserID) IN (
(a.UserAsker, a.UserAsked),
(a.UserAsked, a.UserAsker)
)