sql查询:如何使用标记表过滤父标签和子标签?

发布于 2024-11-28 00:28:56 字数 1387 浏览 1 评论 0原文

从此 post - 我必须进一步过滤行,即我必须确保标签必须标记到页面。

这是我的 root_mm_tagged_pa​​ges 表,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等你爱我 2024-12-05 00:28:56

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文