使用下表:
SELECT i.*, GROUP_CONCAT(t.name) tagList
FROM items AS i
LEFT JOIN tag_rel AS tr ON (tr.item = j.id)
LEFT JOIN tags AS t ON (t.id = tr.tag)
GROUP BY i.id
ORDER BY i.id DESC
有没有办法限制tagList的元素?而不是给我所有的标签(在某些情况下为30+),我可以将其限制为5?
答案 0 :(得分:1)
SELECT i.*, SUBSTRING_INDEX(GROUP_CONCAT(t.name SEPARATOR ','), ',', 5) tagList
FROM items AS i
LEFT JOIN tag_rel AS tr ON (tr.item = j.id)
LEFT JOIN tags AS t ON (t.id = tr.tag)
GROUP BY i.id
ORDER BY i.id DESC
只需用所需数字替换5