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.
Related
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
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
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})
I have 2 tables, a user's table and a friends table.
users
__________
user_id p.k
username
friends
__________
from_user
to_user
status ENUM('1', '2') //1 for sent/pending, 2 for accepted
primary key('from_user', 'to_user') // both also reference user_id
I'm relatively new to MySQL and have been trying to create a search query with the wildcard characters %% that will search based on the username and return rows of usernames, user_id, and status of people that are both friends and non friends to the current user in that order with a limit of 10. I would greatly appreciate any help and guidance on this matter. Below is what I have so far.
SELECT Users.user_id, Users.username, Friends.status
FROM Users
LEFT JOIN
Friends ON
Users.user_id IN Friends.to_user, Friends.from_user AND
'id of user' IN Friends.to_user, Friends.from_user
WHERE
Try this, to get the friends links:
select uf.user_id, uf.username, f.status
from users u
join friends f on u.user_id = f.from_user
join users uf on f.to_user = uf.user_id
where u.username like '%teddy%' and f.status = 2
LIMIT 10;
and this to get all the possible links, friends, sent/pending and not friends:
select uf.user_id, uf.username, f.status
from users u
left join friends f on u.user_id = f.from_user
left join users uf on f.to_user = uf.user_id
where u.username like '%teddy%'
LIMIT 10;
I am working the friend table with mysql. I want to get friend userid and username form current username.
friend
=======
- id
- uid
- fid
Sample Data
===========
id uid fid
1 1 2
2 3 1
user
====
- id
- username
Sample Data
===========
id username
1 saturngod
2 snow
3 John
My current code is
SELECT `user`.id,`user`.username FROM friend
INNER JOIN User
ON user.id = friend.uid
WHERE friend.fid = ( SELECT `id` FROM `User` WHERE `username`='saturngod')
UNION
SELECT `user`.id,`user`.username FROM friend
INNER JOIN User
ON user.id = friend.fid
WHERE friend.uid = ( SELECT `id` FROM `User` WHERE `username`='saturngod')
It's working. I got the friend list. However, I feel , the sql is so long.
Can I reduce this sql and can we write without UNION in sql ?
You have already joined the tables, so the subselect is not needed.
SELECT u.id,u.username
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
WHERE u.username='saturngod'
UNION ALL
SELECT u2.id,u2.username
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
INNER JOIN user u2 ON (u2.id = f.fid)
WHERE u.username='saturngod'
Or you can do:
SELECT
u.id as user_id
,u.username
,u2.id as friend_id
,u2.username as friendname
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
LEFT JOIN user u2 ON (u2.id = f.fid)
WHERE 'saturngod' = u.username
If you want one row per user you can do:
SELECT
u.id as user_id
,u.username
,GROUP_CONCAT(u2.id) AS friend_ids
,GROUP_CONCAT(u2.username) as friendnames
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
LEFT JOIN user u2 ON (u2.id = f.fid)
WHERE 'saturngod' = u.username <<-- optional
GROUP BY u.id
SELECT CASE WHEN user_id="$id" THEN friend_id ELSE user_id END AS friendID
FROM user_friend WHERE user_id="$id" OR friend_id="$id" order by friendID ASC