从这个post得到正确答案后我还有一个问题 - 我必须进一步过滤该行,我必须确保标记必须标记为页面。
这是我的root_mm_tagged_pages table
tag id pg_id
3 11
5 11
6 11
18 12
24 13
26 13
3 14
所以我想要返回这个结果,
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 1
26 tagname-14 0 1
我尝试使用此查询,但收到错误 - #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
如何修复它并进一步过滤结果?
答案 0 :(得分:3)
您在TotalChildren
别名后缺少逗号。
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