sql查询:如何使用标记表过滤父标签和子标签?
从此 post - 我必须进一步过滤行,即我必须确保标签必须标记到页面。
这是我的 root_mm_tagged_pages 表,
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 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 5 行的 'COUNT( tagged.pg_id ) AS TotalTagged FROM root_tags ASparents LEFT OUTER JOI' 附近使用的正确语法
,
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
如何修复它并过滤进一步结果?
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TotalChildren
别名后缺少逗号。You are missing comma after
TotalChildren
alias.