我正在研究从threads
表中获取7个线程的方法,其中包含一个附件(如果在一个线程中发布了多个附件)。我正在使用此查询;
QUERY:
$query = $db->query("
SELECT a.thumbnail, t.tid, t.subject
FROM ".TABLE_PREFIX."attachments a
LEFT JOIN ".TABLE_PREFIX."posts p ON(p.pid=a.pid)
LEFT JOIN ".TABLE_PREFIX."threads t ON(t.tid=p.tid)
WHERE {$where_sql} {$visible}
ORDER BY a.dateuploaded ASC, t.tid DESC
LIMIT 7
");
<小时/> 它确实提取了附件和线程,但问题是它获取了一个线程的所有附件。
有解决方法吗?
凡条款:
if ($mybb->user['usergroup'] == "4")
{
$visible = " AND p.visible = '1' OR p.visible = '0'";
}
else
{
$visible = " AND p.visible = '1'";
}
$where_sql = "t.fid IN({$pdf_fid})";
require_once MYBB_ROOT."inc/functions_search.php";
$unsearchforums = get_unsearchable_forums();
if($unsearchforums)
{
$where_sql .= " AND t.fid NOT IN ($unsearchforums)";
}
$inactiveforums = get_inactive_forums();
if($inactiveforums)
{
$where_sql .= " AND t.fid NOT IN ($inactiveforums)";
}
$permsql = "";
$onlyusfids = array();
// Check group permissions if we can't view threads not started by us
$group_permissions = forum_permissions();
foreach($group_permissions as $fid => $forum_permissions)
{
if($forum_permissions['canonlyviewownthreads'] == 1)
{
$onlyusfids[] = $fid;
}
}
if(!empty($onlyusfids))
{
$where_sql .= "AND ((t.fid IN(".implode(',', $onlyusfids).") AND t.uid='{$mybb->user['uid']}') OR t.fid NOT IN(".implode(',', $onlyusfids)."))";
}
答案 0 :(得分:1)
你实际上工作得很完美,你只需要对threads
进行分组。试试这个:
$query = $db->query("
SELECT a.thumbnail, t.tid, t.subject
FROM ".TABLE_PREFIX."attachments a
LEFT JOIN ".TABLE_PREFIX."posts p ON(p.pid=a.pid)
LEFT JOIN ".TABLE_PREFIX."threads t ON(t.tid=p.tid)
WHERE {$where_sql} {$visible}
GROUP BY t.tid
ORDER BY t.dateline DESC, a.dateuploaded ASC
LIMIT 7
");
答案 1 :(得分:0)
试试这个:
$QUERY = $db->QUERY("
SELECT thumbnail, tid, subject
FROM (SELECT a.thumbnail, t.tid, t.subject
FROM ".TABLE_PREFIX."attachments a
INNER JOIN ".TABLE_PREFIX."posts p ON(p.pid=a.pid)
INNER JOIN ".TABLE_PREFIX."threads t ON(t.tid=p.tid)
WHERE {$where_sql} {$visible}
ORDER BY t.tid DESC, a.dateuploaded DESC
) AS A
GROUP BY tid
LIMIT 7
");