My table has these two ids: IDUser | followingID.
IDUser is the logged in users ID. FollowingID is the ID of the user they want to follow.
I now want to check if their relationship already exists in the following database:
I've tried:
SELECT EXISTS IdUser, followingID FROM following WHERE $id, $followingId"
But that doesn't seem to work.
What would the query be to check if two IDs already exist in a database. Both must exist and must be together in a relationship, not separately. IdUser and followingID are two separate columns showing side by side.
SELECT IdUser, followingID FROM following WHERE IdUser = $id AND $followingId = followingID
Since you want to check if the relationship already exists, you need to check the ID on both columns.
SELECT *
FROM following
WHERE (IDUser = $id AND followingID = $followingID) OR
(followingID = $id AND IDUser = $followingID)
if you want to manipulate the result by only showing YES/NO
for the existing of the relationship,
SELECT IF(COUNT(*) > 0, 'YES', 'NO') Result
FROM following
WHERE (IDUser = $id AND followingID = $followingID) OR
(followingID = $id AND IDUser = $followingID)
You can count the rows where you have such entries.
SELECT count(*)
FROM following
WHERE IdUser = $id
AND followingID = $followingId