检查MYSQL表中是否存在两个ID

检查MYSQL表中是否存在两个ID

问题描述:

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.

我的表有两个ID:IDUser | followingID。 p>

IDUser是登录用户ID。 FollowingID是他们想要关注的用户的ID。 p>

我现在想检查他们之间的关系是否已存在于以下数据库中: p>

I 尝试过: p>

  SELECT EXISTS IdUser,followID FROM WHERE $ id,$ followingId“
  code>  pre> 
 
 

但是 这似乎不起作用。 p>

检查数据库中是否已存在两个ID的查询是什么。两者必须存在且必须在关系中一起存在,而不是单独存在.IdUser 和followingID是两个并排显示的列。 p> div>

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