超级复杂的SQL查询

时间:2011-05-26 19:54:55

标签: mysql

有文章模型,每篇文章都有作者和出版商(都是表格)。用户可以关注作者和发布者。

用户 - >关注 - >作者或出版商 - >制品

我想找到他们所关注的作者和出版商的所有文章。

SELECT articles.*, articles2.* FROM follows 
INNER JOIN articles ON follows.author_id = articles.author_id
INNER JOIN articles AS articles2 ON follows.publisher_id = articles2.publisher_id
WHERE follows.user_id = 1

我可以将所有文章都放入1个查询中吗?如果是这样的话?如果没有,我可以组合两个查询,然后订购它们吗?

5 个答案:

答案 0 :(得分:1)

select a.* from follows f
inner join articles a on (a.author_id = f.author_id or a.publisher_id=f.publisher_id)
where f.user_id = 1

答案 1 :(得分:0)

SELECT articles.*, articles2.* FROM follows 
INNER JOIN articles ON (follows.author_id = articles.author_id OR follows.publisher_id = articles.publisher_id)
WHERE follows.user_id = 1

那应该适合你。

答案 2 :(得分:0)

SELECT articles.* FROM follows,articles
WHERE follows.user_id = 1 
AND (follows.author_id = articles.author_id 
     OR follows.publisher_id = articles2.publisher_id)

答案 3 :(得分:0)

我的头顶:

选择文章。* FROM如下 INNER JOIN文章ON follow.author_id = articles.author_id WHERE following.user_id = 1

UNION

选择文章。* FROM如下 INNER JOIN文章ON follow.publisher_id = articles.publisher_id WHERE following.user_id = 1 按articles.title命令

答案 4 :(得分:0)

选择文章。* FROM跟随INNER JOIN文章ON follow.author_id = articles.author_id

联合所有

SELECT articles2。* FROM跟随INNER JOIN文章AS articles2 ON follow.publisher_id = articles2.publisher_id

类似的东西。