database - Inner join with 3 tables in mysql issue - php

I'm doing a favorite system which allow users to save the posts to their page.
The tables I'm using are:
saved_posts table which contain 3 columns (painned_id``user_id``post_id)
users which contain (user_id, frist_name, last_name, username, email, password, user_website, user_avatar)
Finally posts table (post_id, user_id, post_author, category_id, post_date, post_image, post_avatar, user_website, post_keywords, post_content)
The my sql code I'm using to get the saved posrt
SELECT * FROM posts INNER JOIN saved_posts
ON saved_posts.post_id =
posts.post_id INNER JOIN users on saved_posts.user_id = users.user_id WHERE
saved_posts.user_id = users.user_id AND saved_posts.post_id = posts.post_id
The problem is that the saved posts from one user appears for other user as if they saved them as well.

Shouldn't you filter your answer with a WHERE condition? Otherwise it is just finding all posts that any user shared EVER.
SELECT * FROM posts
INNER JOIN saved_posts
ON saved_posts.post_id = posts.post_id
INNER JOIN users
ON saved_posts.user_id = users.user_id
WHERE users.user_id = :wanted_user_id
Don't forget to bind wanted_user_id.

If I understood the question correctly, the task is to select all the posts which of the users
SELECT * FROM posts p
INNER JOIN saved_posts sp ON p.post_id = sp.post_id
and sp.user_id = u.user_id
INNER JOIN users u on p.user_id = u.user_id

Related

Mysql join on three tables to get list of unique data

I want to show all those users from Users table, whose id is present in table favorite_group_users as user_id for a user whose favorite_groups.id = favorite_group_users.id.
I used below query but it is returning null.
select users.id as user_id, users.first_name as name,
favorite_groups.id as group_id,
favorite_groups_users.user_id as carrier_id
from users
inner join favorite_groups
on users.id = favorite_groups.user_id
inner join favorite_groups_users
on favorite_groups.id = favorite_groups_users.favorite_group_id
where users.id = 38;
You may try nested select
select
favorite_groups.group_id, users_group.user_id
from
favorite_groups_users ,
(select
favorite_groups_users.favorite_group_id,
users.user_id
from
users, favorite_groups_users
where
users.id = 38 and
users.id = favorite_groups.user_id
) users_group
where
users_group.favorite_group_id=favorite_groups.group_id

Mysql select nickNames from other table?

I have 3 tables for a chat app
chatRooms (id)
chatMessages (id, roomId, userId, message)
userData (userId, nickName)
How can I load all the chatRooms with the chatMessages with the nickNames?
I tried:
$qry = $db->prepare('SELECT
chatRooms.id,
chatMessages.message,
userData.nickName
FROM chatRooms
LEFT JOIN chatMessages
ON chatMessages.roomId = chatRooms.id
LEFT JOIN userData
ON chatMessages.userId = userData.userId '
);
$qry->execute();
But it doesn't seem to work for me:C
I would like to display all the users who are in the chatRoom in the chat name
So if 3 people (Fred, Joe, Bane) are in the group then I somehow would need an array with them. For every array element(chatRoom)
Your first LEFT JOIN is more than likely returning more than one row.
From your question, your "chatMessages" table also has a userId in it. You should also filter by this.
In order to do this, you would need to:
Join userData before chatMessages
Include the userId from userData in the LEFT JOIN on chatMessages
$qry = $db->prepare('SELECT
chatRooms.id,
chatMessages.message,
userData.nickName
FROM chatRooms
LEFT JOIN userData
ON chatMessages.userId = userData.userId
LEFT JOIN chatMessages
ON chatMessages.roomId = chatRooms.id AND chatMessages.userId = userData.userId');
$qry->execute();

How to join three MySQL tables with unrelated data?

So I have a webpage that shows an image and information about it, and I also have three tables.
IMAGES USERS COMMENTS
id user_id id
user_id username user_id
username password comment
title isadmin date
image (location) points
description signup_date
points email
category city
bio
profile_image
I need to use all the information from the images and comments table. But I also need to retrieve the profile_image of the user that posted the comment and image. I cannot figure out how to do this: here is what I have so far.
$conn = new PDO ("mysql:host=localhost;dbname=project", "root", "pass");
$stmt = $conn->prepare("
SELECT images.*, users.profile_image, comments.*
FROM (images.id JOIN comments ON images.id = comments.id)
LEFT JOIN users
ON images.user_id = user.profile_image
");
Am I close? or trying the impossible here!?
it's a lot simpler then you'd think :)
the tables are NOT unrelated, they all have the same key - user_id and that's how you join them.
your query should look like this:
SELECT images.*, users.profile_image, comments.*
FROM images
JOIN users ON images.user_id = users.user_id
JOIN comments ON comments.user_id = users.user_id AND images.id = comments.id
this is assuming the images.id and comments.id match (I suspect they do not - but you did not give us enough info)
looking at your table structure, comments and images appear to have no connection, so maybe query them separately to avoid duplicated & confusing data
---UPDATE---
assuming you've added image_id to comments this is your query:
SELECT images.*, users.profile_image, comments.*
FROM images
LEFT JOIN users ON images.user_id = users.user_id
LEFT JOIN comments ON comments.user_id = users.user_id AND images.id = comments.image_id
what LEFT JOIN does is return 'images' that don't necessarily have 'users' or 'comments' connected to them.
I guess this will do the trick:
SELECT i.*, u.profile_image, c.*
FROM images i
LEFT JOIN comments c ON i.user_id = c.user_id
LEFT JOIN users u ON i.user_id = u.user_id
I think it's:
> SELECT images.*, users.profile_image, comments.*
> FROM images
> INNER JOIN users ON images.user_id= users.user_id
> INNER JOIN comments ON users.user_id= comments.user_id

SQL - join selected data or table for conditions

I have SQL tables for users and relationships, like
users{ID, name, email}
relationships{ID, uid, relid, type}
I want to get all "friends" ID, name and email (by current user ID) by SQL, using JOIN or whatever.
If using join is it better to JOIN the table of users and select relationships or select relationships and join users?
something like this (I invented now, i use a bit diferrent tables and code, sorry for errors)
1:
SELECT DISTINCT u.ID, u.name, u.email FROM users u JOIN relationships r ON (r.uid = 1 OR r.relid = 1) AND r.type = 1 WHERE u.ID = r.uid OR u.ID = r.relid
2:
SELECT DISTINCT u.ID, u.name, u.email FROM relationships r JOIN users u ON r.uid = u.ID OR r.relid = u.ID = 1 WHERE r.uid = u.ID OR r.related = u.ID
Edit:
I use DISTINCT becouse when I select users whoose IDs equals to r.uid or r.related, I also get the current user (uid + related) for every relation. DISTINCT should unique the users? I'm beginner and found this on stackoverflow.
So, is it better to select from users and join relations or select relations and join users?
BTW: I quite like the answer with UNION
You can union the users where user is in the uid and in the relid.
SELECT users.*
FROM users
INNER JOIN relationships AS rel
ON users.ID = rel.uid
WHERE users.ID = 500
UNION
SELECT users.*
FROM users
INNER JOIN relationships AS rel
ON users.ID = rel.relid
WHERE users.ID = 500
Not sure why you are using DISTINCT but it looks like you just want to select all the rows from the relationships table (the friends), and then join their details from the users table.
SELECT u.id, u.name, u.email
FROM relationships r
LEFT JOIN users u ON u.id = r.uid
WHERE r.uid = 12345

PHP mysql how to relationship three tables showing outputs from different tables

I have this diagram
What I wanna do is have this output:
How do you manage to do the query of this one?
I have this code
SELECT users.firstname, users.lastname,
users.screenname, posts.post_id, posts.user_id,
posts.post, posts.upload_name,
posts.post_type, posts.date_posted
FROM website.users users
INNER JOIN website.posts posts ON (users.user_id = posts.user_id)
ORDER BY posts.pid DESC
//PROBLEM with this one is that it only views the post from all users.
//SO I added
SELECT COUNT(user_id) AS friends, SUM(user_id = ?) AS you, user_id
FROM feeds WHERE post_id = ?
//This one will give you two fields containing how many different users **feeds** the
post
Please help guys. Actually this one I am just following Facebook's "LIKE" status
the only thing is I'm not an amateur with this kind of stuff so I'd be glad to hear all your answers. I really need your help
If I've understood you correctly, you want an outer join with the feeds table (in order to retain all posts even if there are no associated feeds), then GROUP BY post.pid in order to amalgamate together all such feeds for each post, and SELECT the desired information.
I use MySQL's GROUP_CONCAT() function to obtain a comma-separated list of all users (up to group_concat_max_len) who have
a "feed" for the given post (you can change the delimiter with the SEPARATOR modifier, if so desired).
SELECT users.firstname, users.lastname,
users.screenname, posts.post_id, posts.user_id,
posts.post, posts.upload_name,
posts.post_type, posts.date_posted,
COUNT(feeds.user_id) AS friends, -- number of "likes"
SUM(feeds.user_id = ?) AS you, -- did I like this?
GROUP_CONCAT(feeds.user_id) -- who likes it?
FROM website.users users
INNER JOIN website.posts posts ON (users.user_id = posts.user_id)
LEFT JOIN website.feeds feeds ON (posts.post_id = feeds.post_id)
GROUP BY posts.pid
ORDER BY posts.pid DESC
UPDATE
To obtain the full name of users who have "liked" the post, excluding oneself, one needs to join the users table a second time:
SELECT users.firstname, users.lastname,
users.screenname, posts.post_id, posts.user_id,
posts.post, posts.upload_name,
posts.post_type, posts.date_posted,
COUNT(feeds.user_id) AS friends, -- number of "likes"
SUM(feeds.user_id = ?) AS you, -- did I like this?
GROUP_CONCAT(
CASE WHEN NOT likes.user_id = ? THEN -- exclude self
CONCAT_WS(' ', likes.firstname, likes.lastname) -- full names
END
)
FROM website.users users
INNER JOIN website.posts posts ON (users.user_id = posts.user_id)
LEFT JOIN website.feeds feeds ON (posts.post_id = feeds.post_id)
LEFT JOIN website.users likes ON (feeds.user_id = likes.user_id)
GROUP BY posts.pid
ORDER BY posts.pid DESC
If You want to do it for all the users and simultaneously get the feeds, You have to join this feed table:
SELECT u.firstname, u.lastname,
u.screenname, p.post_id, p.user_id,
p.post, p.upload_name,
p.post_type, p.date_posted,
COUNT(f.user_id) AS friends, SUM(f.user_id = ?) AS you
FROM website.users u
INNER JOIN website.posts p ON (u.user_id = p.user_id)
LEFT JOIN website.feeds f ON (p.post_id = f.post_id)
GROUP BY p.pid
ORDER BY p.pid DESC
This one should do the trick...

Categories