对不起,如果标题与我的问题无关。我不知道这个正确的标题是什么。
所以我有5个表格,以便向用户显示相关内容
Data related content
Data related content's categories
Data related content's publishers
Data related user's followed categories
Data related user's followed publishers
如何根据用户的关注类别和关注的发布者显示内容,以及如果可能的话,如何区分内容是否来自followCategory或followPublisher上的关系
例如:以下是我的查询,用于显示基于用户FollowPublisher cmiiw的内容
SELECT content.id, content.description, ...etc
FROM followPublisher
LEFT JOIN publisher ON followPublisher.publisherId = publisher.id
LEFT JOIN contentPublisher ON publisher.id = contentPublisher.publisherId
RIGHT JOIN content ON contentPublisher.contentId = content.id
WHERE followPublisher.userId = 8
ORDER BY content.created desc;
以下是我的查询,用于根据用户的关注类别显示内容
SELECT content.id, content.description, ...etc
FROM followCategory
LEFT JOIN category ON followCategory.categoryId = category.id
LEFT JOIN contentCategory ON category.id = contentCategory.categoryId
RIGHT JOIN content ON contentCategory.contentId = content.id
WHERE followCategory.userId = 8
ORDER BY content.created desc;
如何结合这两个查询以一次基于用户的关注类别和发布者显示内容,或者有没有比合并查询更好的方法了?
答案 0 :(得分:0)
我认为您需要以下条件:
from pprint import pprint
def fib(n):
if n == 0 or n == 1:
return 1
pprint(f" fib({n - 1})") # f" fib({fib(n - 1)})"
pprint(f" fib({n - 2})")
fib(n - 1) + fib(n - 2)
return fib(n - 1) + fib(n - 2)
fib(5)
答案 1 :(得分:0)
使用联盟
有2个条件
不要在order by
中使用任何与表相关的信息(以idk的说法表示),而应使用as
重命名。在我的情况下,order by content.created
应该更改为content.created as contentCreated
,然后我们可以使用它order by
。
添加默认值列以指示数据的来源(在这种情况下,数据来自followPublisher
还是followCategory
)
查询:
SELECT content.id, content.description, content.created as contentCreated ...etc, 'publisher' as type
FROM followPublisher
LEFT JOIN publisher ON followPublisher.publisherId = publisher.id
LEFT JOIN contentPublisher ON publisher.id = contentPublisher.publisherId
RIGHT JOIN content ON contentPublisher.contentId = content.id
WHERE followPublisher.userId = 8
UNION
SELECT content.id, content.description, content.created as contentCreated ...etc, 'category' as type
FROM followCategory
LEFT JOIN category ON followCategory.categoryId = category.id
LEFT JOIN contentCategory ON category.id = contentCategory.categoryId
RIGHT JOIN content ON contentCategory.contentId = content.id
WHERE followCategory.userId = 8
ORDER BY contentCreated desc;