我有一个像这样的数据库
**member**
id_member (int)
username (varchar)
**topic**
id_topic (int)
topic_name (varchar)
**subscribe**
id_member (int)
id_topic (int)
**post**
id_post (int)
id_topic (int)
title_post (varchar)
我有一个时间轴系统,只显示我关注的主题的帖子,我对SQL Query Left Join感到困惑,任何人都可以帮助我吗?
在订阅表中,它显示每个成员遵循的主题。
答案 0 :(得分:0)
您可以使用加入,如果您已知道id_member
我没有加入会员表,那么只需对其进行过滤,以下查询将为您提供所有主题后跟成员DISTINCT
的所有帖子。用于防止重复结果
SELECT DISTINCT p.* FROM
post p
JOIN subscribe s ON (p.id_topic =s.id_topic )
JOIN topic t ON (p.id_topic =t.id_topic )
WHERE s.id_member ='your id'
/* ORDER BY p.id_post DESC for the latest post to show first if id_post is set to auto_increment*/
编辑这将列出所有来自文章的帖子
SELECT DISTINCT p.* FROM
post p
JOIN
(SELECT * FROM subscribe WHERE id_topic IN (SELECT id_topic FROM topic ) AND id_member !='your id') s
ON (p.id_topic =s.id_topic )
JOIN topic t ON (p.id_topic =t.id_topic )
WHERE s.id_member ='your id'