外部和内部SQL加入

时间:2013-02-27 19:12:33

标签: mysql sql union inner-join

我在将几个sql语句组合成一个更大的单个语句时遇到了麻烦。

问题: 我有一个新闻Feed,需要返回登录用户关注的帐户中的所有帖子。我还需要加入一个表和另外两个表来获取在新闻源上发布的帐户的用户名。我有4个表将在此查询中使用:

关注者(列出具有关注帐户的用户和所关注的帐户类型)

重要列:[user_id,following_id,type]

用户(用户可以关注其他用户帐户)

重要列:[用户名]

服务器(用户也可以关注服务器帐户。用户注意服务器是一对多,因为用户可以创建0个或更多与其帐户绑定的服务器)

重要栏目:[title]

News_Posts (包含消息,帐户类型(整数)和poster_id)

重要列:[poster_id,message,type]

我无法弄清楚如何获取当前登录用户的新闻帖子。

查询1:根据“类型”0 =用户,1 =服务器

返回具有正确用户名的完整新闻列表
    SELECT news_posts.*, users.username FROM news_posts INNER JOIN users ON news_posts.poster_id=users.id WHERE news_posts.TYPE='0'

UNION

    SELECT news_posts.*, servers.title FROM news_posts INNER JOIN servers ON news_posts.poster_id=servers.id WHERE news_posts.TYPE='1'

查询2:返回特定用户(在本例中为id为1)的帐户

    SELECT following_id, type FROM follower WHERE user_id='1'

如何将查询1与查询2组合在一起,以便只有news_post记录WHERE news_posts.poster_id =查询2结果。它必须考虑到关注者的类型,以便使用正确的用户名

编辑:这是架构的粗略版本:http://sqlfiddle.com/#!2/a48f3/1

2 个答案:

答案 0 :(得分:2)

您要求ID 1 的用户关注的帐户(用户或服务器)发布的所有新闻帖子。您需要有关这些帖子的所有信息以及海报的用户名或服务器标题。这是在SQL中,给定您的架构(also at sqlfiddle):

select p.*,
    account.username
from news_posts p
    join 
        (
            select id,
                username,
                0 as type
            from users
            union
            select id,
                title,
                1 as type
            from servers
        ) account
        on (
            account.id = p.poster_id
            and account.type = p.type
        )
    join follower f
        on (
            f.following_id = account.id
            and f.type = account.type
        )
where f.user_id = 1

它的另一个好处是不会两次点击news_posts表。

答案 1 :(得分:0)

你的意思是这样吗?

SELECT * FROM
(
SELECT news_posts.*, users.username FROM news_posts INNER JOIN users ON news_posts.poster_id=users.id WHERE news_posts.type='0'
    UNION
SELECT news_posts.*, servers.title FROM news_posts INNER JOIN servers ON news_posts.poster_id=servers.id WHERE news_posts.type='1'
) un, Follower f
WHERE un.poster_id=f.following_id

修改小提琴:http://sqlfiddle.com/#!2/a48f3/3

如果这不是您想要的,请澄清您的要求。

相关问题