select - MYSQL - returning complete set of tags aftering filtering on specific tags -
i have normalised quotations table , have 4 separate tables pertaining quotes, authors, quotetags (tags classify quotations) , quotestotags (a quotation can have multiple tags e.g. love, happiness, death, etc) 70,000 quotations.
the quotes table consists of fields id, quotation , authorid author table consists of fields id , author (name of author) quotetags consists of fields id , description (e.g. 1 -> happiness, 2 -> admiration) quotestotags consists of id, quoteid, tagid foreign keys quotes table , quotetags table eg (1,1,3 ... 2,1,6....3,2,17..etc)
i want filter quotations series of tags..so if select happiness, love, relationships want select quotations have @ least 1 of these tags associated (alongside id, quotation , author).
now, when filter tags, want return group_concat() of tags each of selected quotations not ones user wants filter by...so if 1 quotation contains 5 tags , 1 of them happiness, want group_concat() contain 5 tags associated it, not ones s/he filtered by.
this problem.i have got work seems return appropriate quotations cant list associated tags. @ moment lists tags filtered :( presume somewhere need exists clause.
so far have this.
select q.*, (select author authors authors.id = q.authorid) authorid, group_concat(qt.description ) tags quotes q left join quotestotags qtt on q.id = qtt.quoteid left join quotetags qt on qtt.tagid = qt.id qt.id in (40,45,70) // arbitrary list of tagids group q.id order q.id asc limit 60, 20
i not know how list tags :(
can please me , point me in right direction? in advance.
also appreciate if show me how filter out quotations contained tags passed filter (the idea being return associated tags each quotation). if happiness , love chosen quotations tagged both happinesss , love selected.
leaving out author id (which doesn't seem important question):
select q.*, group_concat(qt.description ) tags quotes q left join quotestotags qtt on q.id = qtt.quoteid left join quotetags qt on qtt.tagid = qt.id group q.id having sum(qt.id in (40, 45, 70)) > 0 order q.id asc limit 60, 20
the idea use having
clause instead of where
clause filter tags.
Comments
Post a Comment