I have been trying to figure out how to do this, and even with looking at other examples, I can't get it figured out, so maybe I can get some personalized help.
I've got two tables, users_status and friendships.
In the users_status table I have a field userid, and several others.
In the friendships table, I have the fields request_to,request_from, and friendship_status.
Basically what I want to do is get all of the status posts by the current user AND those who are friends of the current user (which I can specify in my PHP using a $userid variable).
Here's an example of the friendships table structure. When a friend request is sent, the userid of the sender and receiver are placed in the table, with a friendship_status of 0. When the request is accepted, the friendship_status is set to 1 and those two are now friends.
friendship_id request_from request_to friendship_status
1 111248 111249 1
2 111209 111249 1
3 111209 111248 0
11 111209 111259 1
5 111252 111209 1
12 111261 111209 1
I realize this may not even be the best structure for determining friendships, especially since the site is relationship based and having to check for friendship connections will be a frequently used thing.
Would it perhaps be better to have two separate tables for friend_requests and friendships? If so, how would I structure/manage the friendships table?
You can use a table join (e.g. http://dev.mysql.com/doc/refman/5.0/en/join.html) to find all of the requests.
Actually you can use a subquery here:
SELECT * FROM users_status WHERE userid = "$userid"
OR userid in (SELECT request_to FROM friendships where request_from = "$userid" AND friendship_status = 1)
OR userid in (SELECT request_from FROM friendships where request_to = "$userid" AND friendship_status = 1)
replace $userid with your user id
The simplest schema I can think of is:
PENDING_FRIENDSHIPS(request_from, request_to)
FRIENDSHIPS(request_from, request_to)
I also removed the ID because both fields on both tables will be compound primary keys (request_from, request_to).
To get all friends from the current user just run:
select * from friendships
where $currentUser = request_from OR $currentUser = request_to
This would return both columns and you would have to remove in PHP the current user.
Another way to get all friends from this schema is to run a UNION:
select request_from from friendships
where request_to = $currentUser
UNION
select request_to from friendships
where request_from = $currentUser
The drawback of this solution is that you're running 2 selects
Related
I have 3 tables :
Person table stores basic person wise details with ID as primary Key
This person can have relationships (father / mother etc), which are saved in Relationship table, however the users for them are created in Person table (e.g. ID = 2,3 in person table), This way we know that 2,3 are related to user 1 (carry).
We also have 3rd table - address, which store user ID wise addresses.(for both a user and his related persons, who are also users)
I want to find out if an address exists for either a user or for his related users in SQL. How to achieve this ?
You can combine two rules and search on the combined table as below
SELECT * FROM
(
SELECT username,id,Address.Address
FROM Person
INNER JOIN Address ON Person.id = Address.Userid
UNION ALL
SELECT username,id,Address.Address
FROM Person
INNER JOIN Relationship ON Relationship.Relatedid = Person.id
INNER JOIN Address ON Relationship.Userid = Address.Userid
) as RES
WHERE Address = 'xyz road'
Also you can find DBFiddle link to workout
Query:
select p.id,p.username,(case when a.userid is null then 'No' else 'Yes'end) IsAddressAvailable
from Person p
left join Address a on p.id=a.Userid
Output:
id
username
IsAddressAvailable
1
Carry
Yes
2
Carry-Father
No
3
Carry-Mother
Yes
db<fiddle here
want mysql query for finding mutual friend between two friend but
I am maintain the friendship of user in one way relationship for ex.
first is users table
id name
1 abc
2 xyz
3 pqr
Now second table is friend
id user_id friend_id
1 1 2
2 1 3
3 2 3
Now here i can say that abc(id=1) is friend of xyz(id=2) now similar way the xyz is friend of abc but now i want to find mutual friend between abc(id=1) and xyz(id=2) that is pqr so I want mysql query for that.
REVISED
This query will consider the "one way" relationship of a row in the friend table to be a "two way" relationship. That is, it will consider a friend relationship: ('abc','xyz') to be equivalent to the inverse relationship: ('xyz','abc'). (NOTE: we don't have any guarantee that both rows won't appear in the table, so we need to be careful about that. The UNION operator conveniently eliminates duplicates for us.)
This query should satisfy the specification:
SELECT mf.id
, mf.name
FROM (
SELECT fr.user_id AS user_id
, fr.friend_id AS friend_id
FROM friend fr
JOIN users fru
ON fru.id = fr.user_id
WHERE fru.name IN ('abc','xyz')
UNION
SELECT fl.friend_id AS user_id
, fl.user_id AS friend_id
FROM friend fl
JOIN users flf
ON flf.id = fl.friend_id
WHERE flf.user IN ('abc','xyz')
) f
JOIN users mf
ON mf.id = f.friend_id
GROUP BY mf.id, mf.name
HAVING COUNT(1) = 2
ORDER BY mf.id, mf.name
SQL Fiddle here http://sqlfiddle.com/#!2/b23a5/2
A more detailed explanation of how we arrive at this is given below. The original queries below assumed that a row in the friend table represented a "one way" relationship, in that "'abc' ff 'xyz'" did not imply "'xyz' ff 'abc'". But additional comments from the OP hinted that this was not the case.
If there is a unique constraint on friend(user_id,friend_id), then one way to get the result would be to get all of the friends of each user, and get a count of rows for that friend. If the count is 2, then we know a particular friend_id appears for both user 'abc' and for 'xyz'
SELECT mf.id
, mf.name
FROM friend f
JOIN users uu
ON uu.id = f.user_id
JOIN users mf
ON mf.id = f.friend_id
WHERE uu.name IN ('abc','xyz')
GROUP BY mf.id, mf.name
HAVING COUNT(1) = 2
ORDER BY mf.id, mf.name
(This approach can also be extended to find a mutual friend of three or more users, by including more users in the IN list, and changing the value we compare the COUNT(1) to.
This isn't the only query that will return the specified resultset; there are other ways to get it as well.
Another way to get an equivalent result:
SELECT u.id
, u.name
FROM ( SELECT f1.friend_id
FROM friend f1
JOIN users u1
ON u1.id = f1.user_id
WHERE u1.name = 'abc'
) t1
JOIN ( SELECT f2.friend_id
FROM friend f2
JOIN users u2
ON u2.id = f2.user_id
WHERE u2.name = 'xyz'
) t2
ON t2.friend_id = t1.friend_id
JOIN users u
ON u.id = t1.friend_id
ORDER BY u.id, u.name
NOTES
These queries do not check whether user 'abc' is a friend of 'xyz' (the two user names specified in the WHERE clause). It is only finding the common friend of both 'abc' and 'xyz'.
FOLLOWUP
The queries above satisfy the specified requirements, and all the examples and test cases provided in the question.
Now it sounds as if you want a row in that relationship table to be considered a "two way" relationship rather than just a "one way" relationship. It sounds like you want to want to consider the friend relationship ('abc','xyz') equivalent to ('xyz','abc').
To get that, then all that needs to be done is to have the query create the inverse rows,, and that makes it easier to query. We just need to be careful that if both those rows ('abc','xyz') and ('xyz','abc') already exist, that we don't create duplicates of them when we invert them.
To create the inverse rows, we can use a query like this. (It's simpler to look at this when we don't have the JOIN to the users table, and we use just the id value:
SELECT fr.user_id
, fr.friend_id
FROM friend fr
WHERE fr.user_id IN (1,2)
UNION
SELECT fl.friend_id AS user_id
, fl.user_id AS friend_id
FROM friend fl
WHERE fl.friend_id IN (1,2)
It's simpler if we don't include the predicates on the user_id and friend_id table, but that could be a very large (and expensive) rowset to materialize.
try this:
given that you want to get the mutual friends of friends 1 & 2
select friend_id into #tbl1 from users where user_id = 1
select friend_id into #tbl2 from users where friend_id = 2
select id, name from users where id in(select friend_id from #tbl1 f1, #tbl2 f2 where f1.friend_id=f2.friend_id)
I need help querying the friendID from a table.
My table stores the user id of two members who are friends together.
But in order to store a "friendhship" b/w two members I would have to store two records like this:
friendshipID | userID | friendID
1 | 5 | 10
2 | 10 | 5
Yet, that seems heavy for the DB when we really only need to store the first record as that is sufficient as it contains both ids of both members.
However, the trouble comes when I want to query the records of the friends of ID=5. Sometimes the ID is in the userID column and other times it is in the friendID column.
This is the query I am using:
SELECT *
FROM friends
WHERE userID = '5'
OR friendID = '5'
But what I want to do is something like this
SELECT
if $userID=5 then userID as myfriend
else friendID=5 then friendID as myfriend
FROM friends WHERE userID='5' OR myfriendID='5'
Hope that makes sense. In the end I would like to have all the friends ID's of member #5 and not bring up results with #5 as the friend or user....but just his friends.
This query would return the Id value, and name, of the friends of #5 as shown in this SQL Fiddle Example
SELECT f.FriendId AS FriendId
, u.Name AS FriendName
FROM FriendTable AS f
INNER JOIN UserAccount AS u ON f.FriendId = u.UserId
WHERE f.UserId = 5
UNION
SELECT f.UserId AS FriendId
, u.Name AS FriendName
FROM FriendTable AS f
INNER JOIN UserAccount AS u ON f.UserId = u.UserId
WHERE f.FriendId = 5
The UNION will remove duplicates, making this query work for both a single record of friends, or the 2 record friendship you mention in the comment. You shouldn't need the 2 record friendship though, because there is no new information being stored in the second record that you cannot get from only having one record.
I've been trying to figure out for a week or so, how I can make a proper friendship system in CakePHP. I've read this and this thread but I can't get it to work.
I've read a lot more threads regarding this, but nobody seems to have a proper example.
I currently have a table users (id, username, password, e-mail etc.) and a table friendships (id, user_to, user_from, status).
Step 1 - Friendship request
If a user does a friendship request, then a row is inserted with the requesting user_id and the user_id of the user from whom the friendship is request, so it could look like:
id | user_from | user_to| status
1 | 1 | 2 | 0
This way I can easily show pending friends of user_id = 2, by selecting all records where user_to = 2
Step 2 - Confirm friendship
I've set it up so that user_id 2 now sees that user_id 1 wants to become friends, if he clicks the confirmation link, the status will be changed to 1, see below
id | user_from | user_to| status
1 | 1 | 2 | 1
I created all kinds of checks so the row stays unique.
Step 3 - Show friends
I thought this would be easy, if I want to show the friends of user_id = 1 then I just do a select with user_from = 1 OR user_to = 1, however this doesn't work.
User_id 1 can be a requester but can also be requested, so a JOIN will show strange results.
Does anyone know a solution? I'm happy to rebuild the entire system if I'm not doing the entire thing right! Any hints in the right direction are welcome as well...
Here is my solution : the difficulty lies in the correct request because friend requests can be crossed (if A asks B or B asks A will be stored the opposite way in the "to" and "from" fields of the table). Lets do it like that and user UNION and aliases to get friends from any user independently of the relation table bellow.
The [friends] table (relation): to|from|statut(pending,confirmed)
"to" and "from" > foreign_keys constraint to a [users] table
The request below always gives the wanted results ! (replace %d by the user ID or user Id in the SESSION
SELECT
users.userNickname,
friends.to AS friendUser,
friends.from AS currentUser,
friends.statut
FROM
users
INNER JOIN
friends
ON
users.userId = friends.to
WHERE
friends.from = '%d'
UNION
SELECT
users.userNickname,
friends.from AS friendUser,
friends.to AS currentUser,
friends.statut
FROM
users
INNER JOIN
friends
ON
users.userId = friends.from
WHERE
friends.to = '%d'
You can find friend requests to ID = 1 this way:
select * from Users u1 where u1.user_to = 1 and u1.user_from not in (select u2.user_to
from Users u2 where u2.user_from = u1.user_to)
You can find friend requests from ID = 1 this way:
select * from Users u1 where u1.user_from = 1 and u1.user_to not in (select u2.user_from
from Users u2 where u2.user_to = u1.user_from)
You can find mutual friendships of ID = 1 this way:
select * from Users u1 where ((u1.from = 1) or (u1.to = 1)) and 0 < (select count(*) from
Users u2 where u1.from = u2.to and u1.to = u2.from)
This code was not tested, but you get the idea.
I'm modifying phpBB's table to have bidirectional relationships for friends. Unfortuntately, people that have already added friends have created duplicate rows:
user1 user2 friend
2 3 true
3 2 true
2 4 true
So I'd like to remove rows 1 and 2 from the example above. Currently, this is my query built (doesn't work atm):
DELETE FROM friends WHERE user1 IN (SELECT user1 FROM (SELECT f1.user1 FROM friends f1, friends f2 WHERE f1.user1=f2.user2 AND f1.user2=f2.user1 GROUP BY f1.user1) AS vtable);
inspired by Mysql Duplicate Rows ( Duplicate detected using 2 columns ), but the difference is that I don't have the unique ID column and I'd like stay away from having an extra column.
Apologies if this isn't 100% legal MySQL, I'm a MSSQL user...
DELETE F1
FROM friends F1
INNER JOIN friends F2
ON F2.user1 = F1.user2
AND F2.user2 = F1.user1
WHERE F1.user1 < F1.user2
DELETE r
FROM friends l, friends r
WHERE l.user1 = r.user2
AND l.user2 = r.user1
This deletes both entries. If you like to keep on of them you have to add a where statement like Will A alread proposed, but i suggest you to use > instead of < to keep the smaller user1 id. Just looks better :)