database - MySQL Limit with Many to Many Relationship -
given schema implementing tags
item itemid, itemcontent
tag tagid, tagname
item_tag itemid, tagid
what best way limit number of items return when selecting tags?
select i.itemcontent, t.tagname item inner join itemtag on i.id = it.itemid inner join tag t on t.id = it.tagid
is of course easiest way them back, using limit clause breaks down, because duplicate of items each tag, counts toward number of rows in limit.
my second solution uses mysql function group_concat() combine tags matching item comma-separated string in result set.
select i.itemcontent, group_concat(t.tagname order t.tagname) taglist item inner join itemtag on i.id = it.itemid inner join tag t on t.id = it.tagid group i.itemid;
the group_concat() function mysql feature, it's not part of standard sql.
Comments
Post a Comment