$query = "SELECT tag.name, tag.id
        FROM #__k2_tags as tag
        LEFT JOIN #__k2_tags_xref AS xref ON xref.tagID = tag.id
        WHERE xref.itemID IN (".implode(',', $IDs).")
        AND tag.published = 1";


在xref.tagID中是标签ID,例如:tagID:42是100倍,tagID:15是90倍,tagID:56是10倍,我想先显示42,然后显示15,然后显示56(我想知道外部参照中有多少标签。 tagID和顺序)

最佳答案

试试这个:

$query = "
SELECT *
FROM (
    SELECT tag.name, tag.id, count(xref.id) AS tagCount
    FROM #__k2_tags as tag
    LEFT JOIN #__k2_tags_xref AS xref ON xref.tagID = tag.id
    WHERE xref.itemID IN (".implode(',', $IDs).") AND tag.published = 1
    GROUP BY tag.name, tag.id
)
ORDER BY tagCount";


子查询中的GroupBy允许您计算每种标记类型的标记出现次数。
需要主查询按汇总值tagCount进行排序。

关于php - 从查询中获取X个最受欢迎的标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45947219/

10-12 14:16