【问题标题】:SQLite: How to see people that aren't my friend, but are friends with my friendsSQLite:如何查看不是我朋友但与我朋友成为朋友的人
【发布时间】:2021-03-21 07:28:39
【问题描述】:

我在 DB Browser 中有一个数据库,其中有两个表;

CREATE TABLE "User" (
    "ID"    integer NOT NULL,
    "name"  TEXT NOT NULL,
    PRIMARY KEY("ID" AUTOINCREMENT)
);

CREATE TABLE "Friends" (
    "UserID"    INTEGER NOT NULL,
    "FriendID"  INTEGER NOT NULL,
    FOREIGN KEY("UserID") REFERENCES "Anvandare"("ID") ON DELETE CASCADE
);

所以我的问题是,如何进行查询以显示我不是朋友的人,但我的朋友是他们的朋友。这样我基本上可以找到新朋友了。

【问题讨论】:

  • 我通过无意的粘贴和张贴撤消了很可能是意外编辑的内容。

标签: sql sqlite inner-join where-clause


【解决方案1】:

您可以多次加入:

select distinct u.id, u.name
from friends f1                                   -- my friends
inner join friends f2 on f2.userid = f1.friendid  -- the friends of my friends
inner join users u on u.id = f2.friendid
where 
    f1.userid = ?
    and f2.friendid <> f1.userid
    and not exists (                               -- that are not my friends
        select 1 from friends f3 where f3.userid = ? and f3.friendid = f2.frienid
    )

【讨论】:

  • 非常感谢,当我运行代码时,它列出了我朋友的所有朋友,但它也列出了想要信息的用户(“我”),我只想列出朋友,我也不是。有没有简单的解决方法?
  • @NotImpostor:我们可以对where 子句使用另一个条件。我更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 2015-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-15
  • 1970-01-01
相关资源
最近更新 更多