这是我的查询
SELECT phpbb_attachments.attach_id, phpbb_attachments.post_msg_id
FROM phpbb_attachments
INNER JOIN phpbb_topics
ON phpbb_attachments.topic_id = phpbb_topics.topic_id
INNER JOIN phpbb_forums ON phpbb_topics.forum_id = phpbb_forums.forum_id
WHERE phpbb_attachments.is_orphan = 0
AND phpbb_attachments.thumbnail = 1
AND (phpbb_attachments.extension = 'jpg'
OR phpbb_attachments.extension = 'png'
OR phpbb_attachments.extension = 'gif')
AND phpbb_forums.forum_id BETWEEN 668 AND 679
ORDER BY phpbb_attachments.attach_id DESC
LIMIT 50
每个主题我只希望获得最新的1个附件。 所以有点像phpbb_attachments.topic_id
答案 0 :(得分:1)
此查询给出每个ropic的“最佳”附件:
SELECT MAX(a.attach_id), t.topic_id
FROM phpbb_attachments a
INNER JOIN phpbb_topics t ON a.topic_id = t.topic_id
INNER JOIN phpbb_forums f ON t.forum_id = f.forum_id
WHERE a.is_orphan = 0
AND a.thumbnail = 1
AND (a.extension = 'jpg' OR a.extension = 'png' OR a.extension = 'gif')
AND f.forum_id BETWEEN 668 AND 679
GROUP BY t.topic_id
然后从该列表中选择附件:
SELECT x.attach_id, x.post_msg_id
FROM phpbb_attachments x INNER JOIN (SELECT MAX(a.attach_id) AS attach_id, t.topic_id
FROM phpbb_attachments a
INNER JOIN phpbb_topics t ON a.topic_id = t.topic_id
INNER JOIN phpbb_forums f ON t.forum_id = f.forum_id
WHERE a.is_orphan = 0
AND a.thumbnail = 1
AND (a.extension = 'jpg' OR a.extension = 'png' OR a.extension = 'gif')
AND f.forum_id BETWEEN 668 AND 679
GROUP BY t.topic_id) i ON x.attach_id = i.attach_id
ORDER BY x.attach_id DESC
LIMIT 50
答案 1 :(得分:0)
SELECT phpbb_attachments.attach_id, phpbb_attachments.post_msg_id
的ID不相同。
现在,即使使用SELECT distinct phpbb_attachments.attach_id, phpbb_attachments.post_msg_id
执行该脚本,它也不会生成distinct
phpbb_attachments.attach_id`。
原因是:
它将使用此字段phpbb_attachments.attach_id, phpbb_attachments.post_msg_id
现在,如果您需要Distinct
的{{1}}
您必须首先做到这一点:
phpbb_attachments.attach_id
然后使用SELECT Distinct phpbb_attachments.attach_id
FROM phpbb_attachments
INNER JOIN phpbb_topics
ON phpbb_attachments.topic_id = phpbb_topics.topic_id
INNER JOIN phpbb_forums ON phpbb_topics.forum_id = phpbb_forums.forum_id
WHERE phpbb_attachments.is_orphan = 0
AND phpbb_attachments.thumbnail = 1
AND (phpbb_attachments.extension = 'jpg'
OR phpbb_attachments.extension = 'png'
OR phpbb_attachments.extension = 'gif')
AND phpbb_forums.forum_id BETWEEN 668 AND 679
ORDER BY phpbb_attachments.attach_id DESC
LIMIT 50
本身将left join
用于特定的不同数据,
phpbb_attachments.attach_id