首页 > 解决方案 > 为什么这个获取朋友的查询没有返回任何结果?

问题描述

我有一个名为 friends 的表,其中包含 3 列:

user1id, user2id, status

Status是另一个表的外键,FriendshipStatuses其他两列都是user表的外键。

我尝试了一些不同的查询,这些查询导致错误并获得仍在请求中的友谊。我提供的查询没有返回任何结果。

$userId包含当前登录用户的 id

SELECT
    users.username, 
    users.fullName, 
    users.user_id 
FROM 
    users 
WHERE 
    users.user_id IN (SELECT user1id, user2id
                      WHERE user1id = $userId 
                         OR user2id = $userId)

标签: sql

解决方案


这段代码没有做任何有用的事情:

SELECT user1id, user2id 
WHERE user1id=$userId 
OR user2id = $userId

您在其中缺少具有列FROM视图user1id的 a和user2id


推荐阅读