我正在开发一个类似于FB的项目。所以我有朋友(存储在“请求”中,通过在2个用户之间请求“响应”为1.我也有“圈子”,就像朋友组。我想要的是一个SELECT,它返回所有朋友而不是在一个给定的圈子里。我相信查询是正确的,直到“AND NOT”,此时我想要排除所有请求(朋友),其中包含发送者或接收者(我知道拼写错误)已经包含在圈子中。
SELECT us.Username ,
ur.Username
FROM Requests r
Join Users us ON us.UserID = r.Sender
JOIN Users ur ON ur.UserID = r.Reciever
WHERE ( Sender = ( SELECT UserID
FROM Users
WHERE Username=?
)
OR Reciever = ( SELECT UserID
FROM Users
WHERE Username = ?
)
)
AND Response=1
AND NOT ON ( SELECT un.Username
FROM CircleMembers c
Join Users un ON un.UserID = c.UserID
WHERE CircleID = ( SELECT CircleID
FROM Circles WHERE Name = ?
)
谢谢!
答案 0 :(得分:3)
此查询应该有效:
SELECT us.Username, ur.Username
FROM Requests r
Join Users us ON us.UserID = r.Sender
JOIN Users ur ON ur.UserID = r.Reciever
WHERE (us.Username = ? OR ur.Username = ?)
AND r.Response = 1
AND NOT EXISTS (
SELECT 1 FROM Circles c
JOIN CircleMembers cm ON c.CircleID = cm.CircleID
WHERE c.Name = ?
AND cm.UserID IN (us.UserID, ur.UserID)
)
答案 1 :(得分:0)
尝试:
AND column_name NOT IN (SELECT column_name FROM table_name WHERE...)