我需要执行以下操作:
接下来,我需要过滤朋友的帖子,并使用检索数据的用户的首选主题标签发布帖子。我认为所有这些都无法在一个查询中完成,对吧?
在最终解决方案中,我需要:
也许更好的解决方案是: 仅选择该帖子,并且对于每个帖子都要进行两次查询以检索用户和媒体数据?
现在这是我用于单篇文章的查询:
PrivilegesRequired=lowest
SELECT posts.id, posts.user_id, posts.privacy, posts.type, posts.published, posts.language,
posts.content, posts.date, posts.entity_map, users.username, media.media_url
FROM posts
LEFT JOIN (SELECT * FROM users) users
ON (users.id = posts.user_id )
LEFT JOIN (SELECT * FROM media ORDER BY media.id DESC) media
ON (media.media_author = posts.user_id AND media.media_type = 'avatar')
WHERE posts.id = ?
是否有20条查询来检索10条帖子的列表?
这里是表格结构:
SELECT * FROM media WHERE media_parent = ?
tbl posts
id
content
date
entity_map
language
privacy
published
raw_content
type
user_id
tbl users
id
name
username
tbl media
id
media_author
media_date
media_parent
media_status
media_type
media_url
答案 0 :(得分:1)
这将为您提供最新的10条帖子。
SELECT posts.id, posts.user_id, posts.privacy, posts.type, posts.published, posts.language,
posts.content, posts.date, posts.entity_map, users.username, media.media_url
FROM posts
LEFT JOIN (SELECT * FROM users) users
ON (users.id = posts.user_id )
LEFT JOIN (SELECT * FROM media ORDER BY media.id DESC) media
ON (media.media_author = posts.user_id AND media.media_type = 'avatar')
Order By post.date desc
limit 10
要获取媒体,您必须对每个帖子进行查询。因此,sql无法像mongodb那样方便引用。