我有四张桌子。。。用户、朋友、文章和文章共享(以书面形式)
用户只需邀请他们阅读,就可以共享他们创建的文章。我试图找到的是文章创建者(id 63)的朋友列表,他们还没有被邀请分享文章(id 34)。
所以,表用户:
user_id - name
10 Dan
11 Doug
12 Chad
13 Ben
63 John
餐桌上的朋友
user_id - friends_id
63 10
63 11
63 12
63 13
餐桌用品
article_id - user_id_creator
34 63
表条款股份
article_id - user_id_sharedWith
34 10
34 11
根据上面的数据,我想返回id为12&13的用户。。查德和本
以下是我正在尝试的:
select u.name, u.user_id, u.live_prof_pic from users u
join friends f on f.user_id = u.user_id
join articles a on a.user_id_creator = f.friends_id
join article_shares as on as.articl_id <> a.article_id
where ((f.friends = '63') && (a.article_id = '34'))
它只是返回0
有什么想法吗?
最佳答案
试试这个,
SELECT c.friends_ID
FROM articles a
INNER JOIN users b
ON a.user_Id_creator = b.user_ID
INNER JOIN friends c
ON b.user_ID = c.user_ID
LEFT JOIN article_shares d
a.article_ID = d.article_ID
WHERE b.user_ID = 63 AND
a.article_ID = 34 AND
d.article_ID IS NULL
关于mysql - SQL查询4种方式联接以查找不存在的人,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12832904/