sql - MySQL: Union, Count, and Group By -
( select root_tags.tag_id, root_tags.tag_name, count( root_tagged.pg_id ) root_tags left join root_tagged on ( root_tagged.tag_id = root_tags.tag_id ) left join root_pages on ( root_pages.pg_id = root_tagged.pg_id ) left join root_granted on ( root_granted.pg_id = root_tagged.pg_id ) root_pages.parent_id = '5' , root_granted.mem_id = '3' group root_tags.tag_id order 3 desc ) union ( select root_tags.tag_id, root_tags.tag_name, count( root_tagged.pg_id ) root_tags left join root_tagged on ( root_tagged.tag_id = root_tags.tag_id ) left join root_pages on ( root_pages.pg_id = root_tagged.pg_id ) root_pages.parent_id = '5' , not exists ( select * root_granted root_granted.pg_id = root_pages.pg_id ) group root_tags.tag_id order 3 desc )
the query above returns result below,
tag_id tag_name count(root_tags.tag_id) 16 expert-category-c 2 14 expert-category-a 1 15 expert-category-b 1 16 expert-category-c 1
as can see tag_id 16
repeated, how can rewrite query tag_id 16
has count number of 3
, mean want query supposed return result this,
tag_id tag_name count(root_tags.tag_id) 16 expert-category-c 3 14 expert-category-a 1 15 expert-category-b 1
i tried query returns error...
( select root_tags.tag_id, root_tags.tag_name, count( root_tagged.pg_id ) root_tags left join root_tagged on ( root_tagged.tag_id = root_tags.tag_id ) left join root_pages on ( root_pages.pg_id = root_tagged.pg_id ) left join root_granted on ( root_granted.pg_id = root_tagged.pg_id ) root_pages.parent_id = '5' , root_granted.mem_id = '3' ) union ( select root_tags.tag_id, root_tags.tag_name, count( root_tagged.pg_id ) root_tags left join root_tagged on ( root_tagged.tag_id = root_tags.tag_id ) left join root_pages on ( root_pages.pg_id = root_tagged.pg_id ) root_pages.parent_id = '5' , not exists ( select * root_granted root_granted.pg_id = root_pages.pg_id ) ) group root_tags.tag_id order 3 desc
could please let me know how make work?
thanks.
the better query given further below, upon analyzing actual query.
you can merge 2 queries using union instead of union (to retain duplicates), run group across entire set.
select tag_id, tag_name, sum(counttags) counttags ( select root_tags.tag_id, root_tags.tag_name, count( root_tagged.pg_id ) counttags root_tags left join root_tagged on ( root_tagged.tag_id = root_tags.tag_id ) left join root_pages on ( root_pages.pg_id = root_tagged.pg_id ) left join root_granted on ( root_granted.pg_id = root_tagged.pg_id ) root_pages.parent_id = '5' , root_granted.mem_id = '3' group root_tags.tag_id union select root_tags.tag_id, root_tags.tag_name, count( root_tagged.pg_id ) counttags root_tags left join root_tagged on ( root_tagged.tag_id = root_tags.tag_id ) left join root_pages on ( root_pages.pg_id = root_tagged.pg_id ) root_pages.parent_id = '5' , not exists ( select * root_granted root_granted.pg_id = root_pages.pg_id ) group root_tags.tag_id ) sq group tag_id, tag_name order counttags desc
since clauses filter against root_granted , root_pages, inner joins. can use exists test emulate first part of union, assuming can never have more 1 root_granted record per root_pages record.
select root_tags.tag_id, root_tags.tag_name, count( root_tagged.pg_id ) counttags root_tags inner join root_tagged on ( root_tagged.tag_id = root_tags.tag_id ) inner join root_pages on ( root_pages.pg_id = root_tagged.pg_id ) root_pages.parent_id = '5' , (not exists ( select * root_granted root_granted.pg_id = root_pages.pg_id ) or exists ( select * root_granted root_granted.pg_id = root_pages.pg_id , root_granted.mem_id = '3')) group root_tags.tag_id, root_tags.tag_name order counttags desc
since not exists
, exists
mutually exclusive, can combine them using or single query.
Comments
Post a Comment