I am coding a friendship system and it has two tables.
members
id
username
password
friends
id
user_id
friend_id
status
Let's say that i want a query that can select the friends IDs of the member $userId how possible to make this in one query?
I found a solution which is to make 2 queries. The fist selects the friends WHERE user_id = $userId AND the second selects friends WHERE friend_id = $userId and then MIX them in one array. If there is no other solution I'm going to use it.
please any ideas for both the SQL structure & Queries?
Use:
SELECT f.friend_id
FROM FRIENDS f
WHERE f.user_id = $user_id
UNION
SELECT t.user_id
FROM FRIENDS t
WHERE t.friend_id = $user_id
Using UNION will remove duplicates. UNION ALL would be faster, but it doesn't remove duplicates.
If you want to get the information for the friends from the MEMBERS table, use:
SELECT m.*
FROM MEMBERS m
JOIN (SELECT f.friend_id 'user_id'
FROM FRIENDS f
WHERE f.user_id = $user_id
UNION
SELECT t.user_id
FROM FRIENDS t
WHERE t.friend_id = $user_id) x ON x.user_id = m.id
BTW: I hope you're using mysql_escape_string on the variables, otherwise you risk SQL injection attacks:
You should be able to try using
SELECT m.*
FROM friends f INNER JOIN
members m ON f.friend_id = m.user_id
WHERE f.user_id = $userId
This will give you all the Friends details
To get BOTH have a look at
SELECT DISTINCT CASE WHEN f.user_id = $userId then f.friend_id else f.user_id END CASE
FROM friends f
WHERE f.user_id = $userId
OR f.friend_id = $userId
Why not inserting 2 rows for 1 friendship. For example:
Let's say we have 2 user will become friends
User_id : 1 &
Friend_id : 2
insert into friends (user_id, friend_id, status) values (1,2,0)
insert into friends (user_id, friend_id, status) values (2,1,0)
so you can select easily by simple select query.
Also it will ease the pain for your likely next question "How to find Mutual Friends".
Since you're asking for something simple like:
SELECT friend_id FROM friends WHERE user_id = id; [fill in the id]
I'll give you something fancier:
SELECT * FROM members AS m
WHERE m.id
IN (SELECT f.friend_id FROM friends AS f
WHERE f.user_id = (SELECT pm.id FROM members AS pm
WHERE pm.username = 'amindzx'));
Granted using a join over a sub-query would be better.
Also, there's no need for an id in the friends column, because only one relationship between a user_id and a friend_id should exist; these can both be described as the id columns in unison.
Related
I am working on social networking site.
I've three tables one is user table which is used to store user details, another table is follow table which is used for followers following list.
In this table I am storing user_id and follower_id.
Third table is user_friends in this I'm storing user_id and friend_userid.
I want to search the user from my friends list and follow list. For this i've written query like this:-
select f.follower_id,uf.friend_userid,u.user_id,u.first_name,u.last_name from tbl_user u
LEFT JOIN tbl_userfriends uf ON uf.friend_userid = u.user_id
LEFT JOIN tbl_follow f ON f.follower_id = u.user_id
where uf.friend_userid != '11'
AND u.first_name LIKE '%a%'
This query returning users only who are friends it is not returning the follow users.
Any help will be appreciated. Thanks in Advance.
You're joining tbl_follow on the Follower ID being equal to the User ID. I suspect that's probably not right.
If you don't already have one you'll need a user id key in the follower table to join on, then you can change your join to;
LEFT JOIN tbl_follow f ON f.userid = u.user_id
i've done this by using following query:-
select u.user_id,u.first_name,u.last_name from tbl_user u LEFT JOIN tbl_userfriends uf ON uf.friend_userid = u.user_id LEFT JOIN tbl_follow f1 ON f1.follower_id = u.user_id LEFT JOIN tbl_follow f2 ON f2.user_id = u.user_id where (uf.user_id = '11' OR f1.user_id = '11' OR f2.follower_id = '11') AND (u.first_name LIKE '%s%' OR u.last_name LIKE '%s%') AND u.status = '0' group by u.first_name
This query returning me all the users who are my followers, friends and to whom i am following.
i have two tables in my database one is to keep users info (users_table )
and the other one keeps track of the friends
users_table:
id username avatar
1 max max.jpg
2 jack jack.jpg
friends_table :
id u1_id u2_id
1 1 2
2 1 3
in every user profile i show his/her friends list
here is my query
select u.id,
u.username,
u.avatar
from friends_table f
join users_table u on f.u1_id = u.id || f.u2_id = u.id
where u.id <> $profile_id
and (f.u1_id = $profile_id || f.u2_id = $profile_id)
this query selects friends of the profile owner ($profile_id)
and join them with the user table to get each friend username and avatar
now i want to count the mutual friends between each friend and the profile owner is it possible to this in one query or should i do some long and probably slow query like this for each founded friend( it's just a example and it might have some syntax error ):
foreach ( $friends_list_query_resul as $qr ){
$friend_id = $qr['id'];
$mutual_count = mysql_query
( "select count(*) from friends_table where
($u1_id = $friend_id || $u2_id = $friend_id )
&&
( $u1_id IN ( SELECT `u1_id`,`u2_id` from friends_table where
($u1_id = $profile_id || $u2_id = $profile_id ) )
||
$u2_id IN ( SELECT `u1_id`,`u2_id` from friends_table where
($u1_id = $profile_id || $u2_id = $profile_id ) )
")
}
Your first query could also be written as:
select distinct u.id,
u.username,
u.avatar
from users_table u where u.id in
(select case when u1_id=$profile_id then u2_id else u1_id end
from friends_table f where case when u1_id=$profile_id
then u1_id else u2_id end =$profile_id);
The mutual friends query can be written as a single query in similar fashion:
select u.id, (select count(f.id) from friends f where
case when f.u1_id=u.id then u2_id else u1_id end in
(select distinct case when u1_id=$profile_id then u2_id else u1_id end
from friends where case when u1_id=$profile_id then u1_id else u2_id
end =$profile_id)
and u1_id=u.id or u2_id=u.id and
(u1_id <> $profile_id and u2_id <> $profile_id))
as mutual_frnds from user u where u.id <> $profile_id;
but you might want to performance test either of them before using.
All you need is one query:
select id, username, avatar, -- ...
(
select count(*)
from friends_table f1
inner join friends_table f2 on f1.u2_id = f2.u1_id and f2.u2_id = f1.u1_id
where f1.u1_id = users_table.id
)
as mutual_friend_count
from users_table
The meaning of the subquery is:
Give me the count of the "friend of a friend" relations the user participates in, such that the target of the first friend relation is the source of the second friend relation, and the target of the second friend relation is the source of the first one.
First, I do not understand why so complicated query to retrieve the user's friends... It should be simply achieved by this query:
select u.id,
u.username,
u.avatar
from friends_table f
left join users_table u on f.u2_id = u.id
where f.u1_id = $profile_id
Explain: the logged in user is the one whose id is the same as f.u1_id. Therefore we only select friends whose ids are in f.u2_id.
Then, to count the mutual friends of my friends we can use query like this:
select count(*) as mutual_count, f.u1_id as mutual_friend_id
from friends_table f
where f.u1_id IN (select f.u2_id from friends_table where f.u1_id = {$profile_id})
where $profile_id is the ID of logged in user...
Is this correct?
i've decided to add two rows for each friend relation to the table .
id u1_id u2_id
1 10 20
2 20 10
it makes the process easier and faster .
I have an application, very basic description:
Users login -> they post "activities" (like snowboarding)
Users have friends
Users can bind multiple friends to activities
Users can "like" activities created by friends
What I need is a query to check if the user is allowed to "like" an activity by one of their friends. They are only allowed when one of the users friends was bound to the target activity.
users: id, name
usersFriends: id, uid, friendUid
activities: id, description
activitiesUsers: id, activityId, uid
activitiesLikes: id, activityId, uid
I hope someone can help me with this query, and if possible to return true or false. I hope my question is clear and thanks for your time :)
This should produce a list of activities.id for all of the user's friends.
SELECT
activities.id AS canLikeId
FROM
users u
JOIN usersFriends uf ON u.id = uf.uid
JOIN activitiesUsers au ON uf.friendId = au.uid
JOIN activities a ON a.id = au.activityId
Wrapped in an EXISTS, it looks like:
SELECT activities.id FROM activities aCanLike
WHERE EXISTS (
SELECT
a.id AS canLikeId
FROM
users u
JOIN usersFriends uf ON u.id = uf.uid
JOIN activitiesUsers au ON uf.friendId = au.uid
JOIN activities a ON a.id = au.activityId
WHERE a.id = aCanLike.id
)
Or something with an IN() clause that attempts to get everything from activities owned by any of the user's friends.
SELECT
activities.*
FROM activities JOIN activitiesUsers ON activities.id = activitiesUsers.activityId
WHERE activitiesUsers.uid IN (
SELECT friendUid FROM usersFriends WHERE uid = $userid
)
Assuming you know the user's id and activity id at the time of the query, you could do something like:
SELECT COUNT(*) FROM activitiesUsers WHERE activityId = 'xx' AND uid IN (SELECT friendUid FROM usersFriends WHERE uid = 'xx')
Should return 0 if none of their friends are bound to the activity or a positive number if they have friends bound to that activity ...
This should do it:
select
count(*) as CanLike -- 0 if false, >= 1 if true
from
usersFriends uf
join
activitiesUsers au
on
uf.friendUid = au.uid
where
au.id = $activityId
and
uf.uid = $userId
SELECT COUNT(*) AS allowed
FROM userFriends
JOIN activitiesUsers ON (userFriends.uid = activitiesUsers.uid
AND activitiesUsers.activityId = $activity_id)
WHERE userFriends.uid = $user_id
I'm struggling with this for hours so please help me.
This is my users table
id | username | last_activity(timestamp for online system)
And this is my friends table
id | uid | fid
What I want is to order the output by last_activity
My current query looks like this:
SELECT fid FROM friends WHERE uid='$user_id'
SELECT f.fid FROM f.friends
LEFT JOIN users u ON f.uid = u.id
WHERE uid=$user_id
ORDER BY u.last_activity DESC
You want to use INNER JOIN to join the two tables:
SELECT f.fid
FROM friends f
INNER JOIN users u
ON u.id = f.id
ORDER BY u.last_activity DESC
Always make sure you type out a real JOIN clause as some ways are old and getting more and more unsupported.
Read more here:
INNER JOIN on w3schools
I think you want
SELECT f.fid AS fid
FROM friends f, users u
WHERE f.uid = u.id AND f.uid = $user_id
ORDER BY u.last_activity DESC
I assume your problem is that fid is on one table, but the ordering criterion last_activity is on another table. The goal of this query is to JOIN each row in the friends table with the corresponding row in the users table (via the WHERE clause).
Assuming uid in friends table is foreign key references to id in users
table. so using INNER JOIN you can retrieve your desired results.
SELECT f.fid
FROM friends f INNER JOIN user_tb u
ON u.id = f.uid
WHERE f.uid = '$user_id'
ORDER BY u.last_activity DESC;
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Self referencing relation with followers|friends users
Hi there.
I have two tables called users and users_friends.
users:
id INT
username VARCHAR
.....
users_friends:
user_id INT
friend_id INT
PRIMARY KEY (`user_id`,`friend_id`)
How can I get the information about a specific users friends? Let's say I want to list friends associated with the user with an id of 5.
Can't get my joins working, everything I try ends up with no result. Any ideas? This should be I simple query with two INNER JOIN I think but can't manage.
Then a second question is how do I structure this using Kohana 3.1 ORM? Perhaps I shouldn't?
EDIT: Anyone have an idea about doing this with Kohana 3 ORM?
What about something like this :
select users.*
from users
inner join users_friends on users_friends.friend_id = users.user_id
where users_friends.user_id = 5
SELECT *
FROM `users` AS u
INNER JOIN `users_friends` AS uf ON u.id = uf.user_id
WHERE u.id = '5'
this is the query with INNER JOIN
if all of the information is stored in users you will need to join users once more
SELECT ufu.*
FROM `users` AS u
INNER JOIN `users_friends` AS uf ON u.id = uf.user_id
INNER JOIN `users` AS ufu ON uf.friend_id = ufu.user_ud
WHERE u.id = '5'
Ofcourse a JOIN is possible, but that wont be faster then a simple query like:
SELECT *
FROM users
WHERE id IN (
SELECT friendID FROM user_friends WHERE userID=5
)
But, this only lists the friends from user 5. If you want the other way around, so list the people that say user 5 is a friend, this query is also easier to alter.
SELECT *
FROM users
WHERE id IN (
SELECT userID FROM user_friends WHERE friendID=5
)
isn't it a simple:
Select friend_id from users_friends where user_id = 5
5 being the users id.
Off the top of my head:
SELECT friend_id, username
FROM users_friends JOIN users
ON users_friends.friend_id = users.id
WHERE user_id = 5;