开发者

sql query: how to filter parent and children tags with a tagged table?

I have a further problem after getting the correct answer from this post - I have to filter the row a bit further which is I have to make sure the tag must be tagged to a page.

This is my root_mm_tagged_pages table

tag id  pg_id 
3       11  
5       11  
6       11  
18      12  
24      13  
26      13
3       14

So I want to return this result,

ParentID    ParentName  TotalChildren   TotalTagged
3           Tagname-1   2               2
5           tagname-2   2               1
6           tagname-3   1               1
18          tagname-10  0               1
24          tagname-13  0     开发者_JAVA百科          1
26          tagname-14  0               1

I tried with this query but I get an error - #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'COUNT( tagged.pg_id ) AS TotalTagged FROM root_tags AS parents LEFT OUTER JOI' at line 5 ,

SELECT 
    parents.tag_id AS ParentID,
    parents.tag_name AS ParentName,
    COUNT(childs.tag_id) AS TotalChildren
    COUNT( tagged.pg_id ) AS TotalTagged

FROM root_tags AS parents
    LEFT OUTER JOIN root_tags AS childs
    ON parents.tag_id = childs.parent_id

    LEFT OUTER JOIN root_mm_tagged_pages AS tagged
    ON tagged.tag_id = parents.tag_id

WHERE parents.parent_id IS NULL
GROUP BY parents.tag_id, parents.tag_name
ORDER BY parents.tag_id

How can I fix it and filter the result further?


You are missing comma after TotalChildren alias.

SELECT 
    parents.tag_id AS ParentID,
    parents.tag_name AS ParentName,
    COUNT(childs.tag_id) AS TotalChildren , --You missed the comma here
    COUNT( tagged.pg_id ) AS TotalTagged
FROM root_tags AS parents
    LEFT OUTER JOIN root_tags AS childs
    ON parents.tag_id = childs.parent_id

    LEFT OUTER JOIN root_mm_tagged_pages AS tagged
    ON tagged.tag_id = parents.tag_id

WHERE parents.parent_id IS NULL
GROUP BY parents.tag_id, parents.tag_name
ORDER BY parents.tag_id
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜