I have a database that holds one entry for all unique "friendship" pairs. I need a query that will return a single row that matches the pairs. For Example:
friend_id_one = "10987633" friend_id_two = "01223995"
------------- -------------
|id_one |id_two |
------ ------ -------------
|19955220 |01223995 |
------ ------ -------------
|01223995 |10987633 | <--- Correct Row
------ ------ -------------
|01223995 |00254987 |
------ ------ -------------
|01223995 |99774444 |
------ ------ -------------
|78554412 |01223995 |
------ ------ -------------
The output should be:
------------- -------------
|id_one |id_two |
------ ------ -------------
|01223995 |10987633 | <--- Correct Row
------ ------ -------------
My issue is that multiple users can have multiple unique friend pairs. The first ID is always the users that initiates the friendship. But, the order of the IDs when input can be reversed. Say, the friend whose ID belongs to id_two submits a query, the order of the variables would be inverse.
Example: friend_one gives a gift to friend_two. The first ID is always the person taking action, therefore the argument order would be friend_one and friend_two. But, if friend_two gives the gift the argument order would be friend_two and friend_one.
To sum up, I need to return as single row containing unique pairing for this to work properly. I have tried this:
SELECT * FROM friends WHERE
(friend_one_id=? OR friend_two_id=?) AND (friend_one_id=? OR
friend_two_id=?), (friend_one.user_id, friend_two_id,
friend_two_id,friend_one.user_id,)
This obviously failed and returned nothing. Scratching my head as to how this might work.
CodePudding user response:
I was actually very close to solving this. The solution was:
SELECT * FROM friends WHERE
(friend_one_id=? AND friend_two_id=?) OR (friend_one_id=? AND
friend_two_id=?), (friend_one.user_id, friend_two_id,
friend_two_id,friend_one.user_id,)