SQLite:如何查看不是我朋友但与我朋友是朋友的人
SQLite: How to see people that aren't my friend, but are friends with my friends
我在 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
);
所以我的问题是,如何进行查询以显示我不是朋友但我的朋友是他们的朋友的人。这样我基本上就可以找到新朋友了。
您可以多次加入:
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
)
我在 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
);
所以我的问题是,如何进行查询以显示我不是朋友但我的朋友是他们的朋友的人。这样我基本上就可以找到新朋友了。
您可以多次加入:
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
)