SQL displaying data from one table based on id from another table - mysql

I am new at SQL I a need help in creating one.
I have 2 tables:
table 1:
table2:
And now I want first name and last name for all friends of one user example of user John Doe based on id from user and friend user id from table friends?

You should do 2 joins, one to get each friend's ID and another to retrieve that friend's name.
SELECT
U.ID,
U.first_name,
U.last_name,
N.first_name FriendFirstName,
N.last_name FriendLastName
FROM
[user] U
LEFT JOIN firends F ON U.ID = F.user_id
LEFT JOIN [user] N ON F.friend_id = N.id
Using a LEFT JOIN will make you see people with no friends (sniff).
If you want to see a particular user:
SELECT
U.ID,
U.first_name,
U.last_name,
N.first_name FriendFirstName,
N.last_name FriendLastName
FROM
[user] U
LEFT JOIN firends F ON U.ID = F.user_id
LEFT JOIN [user] N ON F.friend_id = N.id
WHERE
U.ID = 1948 -- Supplied ID

Do the self joins
select f.first_name, u.first_name as friend_name
from user u
join (
select u.first_name, f.friend_id
from friend f
join user u on u.id = f.user_id
) f on f.friend_id = u.id

A simple inner join will do
Select First_name,Last_name
from user inner join friends on user.id = friends.user_id
where friends.id = 1
Or you can replace with friends.user_id = 1 if you would like to look at user_id column

maybe I'm getting it wrong, but this is my suggeston:
SELECT
u1.first_name,
u1.last_name,
u2.first_name AS FriendFirstName,
u2.last_name AS FriendLastName,
FROM user AS u1
INNER JOIN friends AS f
ON t1.id = f.user_id
INNER JOIN user AS u2
ON f.friend_id = u2.id

Related

SQL query to find all users, User 'A' follows and then check if another User 'B' also follows the same users

I want to display all users data, who User 'A' is following. And then further check if User 'B' is also following some users of User 'A'.
I managed to get al users data, who User 'A' is following. But don't understand how to query for the second condition.
Here is my Fiddle link with an example: https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=29a7d1e29f794a8f18a89fe45c06eaa9
You can try to let your User 'B' in a subquery then do OUTER JOIN
SELECT u.*,
IF(friend_id IS NULL,0,1) amIfollowing
FROM users u
LEFT JOIN (
Select friend_id
from friends
where user_id = 5
) f ON f.friend_id = u.id
WHERE u.id IN (SELECT f.friend_id
FROM friends f
WHERE f.user_id = 1)
ORDER BY u.id
sqlfiddle
If I understand correctly you can try to use only one subquery for friends and then use the condition aggregate function to get the result.
SELECT u.id,
u.image_width,
MAX(CASE WHEN f.user_id = 5 THEN 1 ELSE 0 END) amIfollowing
FROM users u
JOIN (
Select friend_id,user_id
from friends
where user_id IN (1,5)
) f ON f.friend_id = u.id
GROUP BY u.id,
u.image_width
ORDER BY u.id
You could use exists here to check if the corresponding IDs exist:
SELECT *,
case when exists (
select * from friends f
where f.friend_id = u.id and f.user_id = 5
) then 1 else 0 end amIfollowing
FROM users u
WHERE u.id IN (SELECT f.friend_id
FROM friends f
WHERE f.user_id = 1);
Example Fiddle
Looks like a JOIN will do, with distinct
SELECT distinct u.*, (f2.user_Id is not null) amIfollowing
FROM users u
JOIN friends f ON u.id = f.friend_id
LEFT JOIN friends f2 on f2.friend_id = f.friend_id and f2.user_id = 5
WHERE f.user_id = 1
ORDER BY u.id

Filter joined results

Imagine the following table structure:
tb.user tb.usergroups tb.group
id id id
name user_id name
group_id
A user can be in multiple groups through the usergroups table.
I want to search for a user by user name and group name. Speaking in examples: user "Bob" is in "GroupA" and in "GroupB". My query so far:
SELECT *
FROM user u
JOIN usergroups ug ON ug.user_id = u.id
JOIN group g ON ug.group_id = g.id
WHERE u.name = 'Bob' AND g.name != 'GroupA'
This isn't exactly what I want; my result still contains Bob, since he's in "GroupB", but I don't want the result to contain Bob at all.
How to do that?
Thanks
I understand you want to select all users named "Bob" that are not in group A but are in group B. This should do it :
SELECT b.*
FROM (SELECT * FROM user u
JOIN usergroups ug ON ug.user_id = u.id
JOIN group g ON ug.group_id = g.id
WHERE u.name = 'Bob' AND g.name = 'GroupB') b
LEFT JOIN (SELECT userid FROM user u
JOIN usergroups ug ON ug.user_id = u.id
JOIN group g ON ug.group_id = g.id
WHERE u.name = 'Bob' AND g.name = 'GroupA') a
ON a.userid=b.userid
WHERE a.userid is NULL
Check this query as i understand you are looking something like this:
SELECT * FROM user AS u
LEFT JOIN usergroups AS ug ON u.id = ug.user_id
LEFT JOIN groups AS gp ON gp.id = ug.group_id
WHERE u.name = 'Bob'
GROUP by ug.user_id
HAVING count(ug.group_id) = 1 AND gp.name !="GROUPA"

'friends of friends' SQL query

This question is related to previous one. You can check my first post here
I'm trying to pull data from a user table and I need 'friends of friends', those who are two steps away from the chosen user but not directly connected to the chosen user
I tried with this query:
select
u.*
from user u
inner join friend f
on u.user_id = f.friend_id
inner join friend ff
on f.user_id = ff.friend_id
where ff.user_id = {$user_id} AND u.user_id <> {$user_id};
I didn't know how to pull users who are not directly connected to the chosen user. I get all friends of friends of the current user, but I also get direct friends of the current user.
You just need to exclude the ones who are direct friends as well as being friends-of-friends. I've rearranged the table aliases so it's a bit clearer (to me, anyway) what's being retrieved:
SELECT
u.*
FROM
user u
INNER JOIN friend ff ON u.user_id = ff.friend_id
INNER JOIN friend f ON ff.user_id = f.friend_id
WHERE
f.user_id = {$user_id}
AND ff.friend_id NOT IN
(SELECT friend_id FROM friend WHERE user_id = {$user_id})
It also removes the need to exclude the user ID being queried.
You are on the right track. You need to add a where condition that excludes those who are directly related:
select u.*
from user u
inner join friend f on u.user_id = f.friend_id
inner join friend ff on f.user_id = ff.friend_id
where ff.user_id = {$user_id} AND u.user_id <> {$user_id};
AND not exists
(select f2.friend_id
from friend f2
where f2.friend_id = ff.friend_id
and u.user_id = f2.user_id)
The extra not exists clause that I added to your query verifies that the second degree friend is also not a first degree friend.
I'd use a non-correlated subquery, which retrieves the ids of the direct friendships of {$user_id}, and exclude these users from the final result:
select u.*
from user u
inner join friend f on u.user_id = f.friend_id
inner join friend ff on f.user_id = ff.friend_id
where ff.user_id = {$user_id} AND u.user_id <> {$user_id}
AND u.user_id not in (
select directFriend.friend_id
from friend directFriend
where directFriend.user_id = {$user_id})

Selecting all friends from mysql database

I got a database with with users and their connection as friends.
I just want to request simply all friends of a user with their username and the status of their friendship where status can be 0 for pending or 1 for accepted.
The Tables are:
users user_friend friend
- id - id -id
- username - friend_id
- user_id
- status
I just cant get the query right... Maybe someone can help me :)
What i want to get at the end is:
user_id username status
What i tried is: (even if its totally wrong x))
SELECT u.id, u.username, uf.friend_id, uf.status
FROM friends f
INNER JOIN user_friend uf ON uf.user_id = :user_id AND uf.friend_id = f.id
INNER JOIN users u ON u.id = uf.user_id
First you can DROP table friends.
Then friends for a particular user (id = 1234):
SELECT
CASE uf.user_id
WHEN 1234 THEN u.username
ELSE f.username END as friend_name,
CASE uf.user_id
WHEN 1234 THEN u.id
ELSE f.id END as friend_id,
uf.status
FROM
user_friends uf
JOIN
users f
ON uf.user_id = u.id AND uf.friend_id = 1234
JOIN
users u
ON uf.friend_id = f.id AND uf.user_id = 1234
Based on your data layout, you would seem to want something like this:
select f.*
from user u join
userfriends uf
on uf.user_id = u.id join
friend f
on uf.friend_id = f.id
where u.username = XXX;
Your data structure seems strange. Normally, there would be no friends table. Users would just befriend each other.
SELECT u.UserId, f.FriendId, uf.Status
FROM Friend f
INNER JOIN UserFriend uf
ON f.FriendId = uf.FriendId
INNER JOIN Users u
ON uf.UserId = u.UserId WHERE u.UserId = '1'
This gets all UserId, FriendId and Status from a certain UserId.

mysql left join exists

Tables:
users: id, name
usersFacebookFriends: id, uid, friendUid, name
I need a query to search for friends (name), and check if they exists in the users table, so then they are also on the website registered.
Is this possible with a left join, and if so how?
Thanks.
SELECT ff.id, ff.uid, ff.friendUid, ff.name, u.id, u.name
FROM usersFacebookFriends ff
LEFT JOIN users u
ON ff.name = u.name;
If you want only those that exist:
SELECT ff.id, ff.uid, ff.friendUid, ff.name, u.id, u.name
FROM usersFacebookFriends ff
INNER JOIN users u
ON ff.name = u.name;
If you want only those that don't exist:
SELECT ff.id, ff.uid, ff.friendUid, ff.name, u.id, u.name
FROM usersFacebookFriends ff
LEFT JOIN users u
ON ff.name = u.name
WHERE u.id IS NULL;
I guess this should make a job. If facebook friend is not registered with your site corrsponding joined User table fields will be NULL.
SELECT f.name, u.* FROM usersFacebookFriends AS f
LEFT JOIN users AS u
ON f.friendUid = u.id
WHERE f.uid = 123
Change LEFT JOIN to INNER JOIN if you need to find only those who exist.