当前位置: 动力学知识库 > 问答 > 编程问答 >

mysql - Limiting number of LEFT JOIN group

问题描述:

With the following table:

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

is there a way to limit the elements of tagList? Instead of giving me all the tags (30+ in some cases), could I just limit it to i.e. 5?

网友答案:
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

Just replace 5 with the number desired

分享给朋友:
您可能感兴趣的文章:
随机阅读: