繁体   English   中英

SQL查询:如何使没有孩子的标签成为父母?

[英]sql query: how to make the tags without children to become parents?

我有这个标签表,

tag_id  tag_name    parent_id   cat_id
3       Tagname-1   NULL        NULL    
5       tagname-2   NULL        NULL    
6       tagname-3   NULL        NULL
9       tagname-4   NULL        NULL
11      tagname-5   3           NULL
13      tagname-6   3           NULL
15      tagname-8   5           NULL
17      tagname-9   5           NULL
18      tagname-10  NULL        NULL
20      tagname-11  6           NULL
22      tagname-12  9           NULL
24      tagname-13  NULL        NULL
26      tagname-14  NULL        NULL
28      tagname-15  NULL        NULL

我想返回这样的结果,

ParentID    ParentName  TotalChildren
3           Tagname-1   2
5           tagname-2   2
6           tagname-3   1
9           tagname-4   1
18          tagname-10  0
24          tagname-13  0
26          tagname-14  0
28          tagname-15  0

所以在这里我到目前为止提出的查询,

SELECT 
a.tag_id as ParentID,
a.tag_name as ParentName,
b.TotalChildren

FROM root_tags a INNER JOIN
(
    SELECT parent_id, COUNT(1) as TotalChildren
    FROM root_tags
    WHERE parent_id <> tag_id
    GROUP BY parent_id
) b 

ON a.tag_id = b.parent_id
ORDER BY ParentID

但是,不幸的是,它只会返回这样的结果,

ParentID    ParentName  TotalChildren
3           Tagname-1   2
5           tagname-2   2
6           tagname-3   1
9           tagname-4   1

这意味着它缺少了没有孩子的父母。

没有孩子也要怎么做才能成为父母? 或者换句话说,如何让没有父母的标签自己成为父母?

编辑:

SELECT 
a.tag_id as ParentID,
a.tag_name as ParentName,
b.TotalChildren

FROM root_tags a LEFT OUTER JOIN
(
    SELECT parent_id, COUNT(1) as TotalChildren
    FROM root_tags
    WHERE parent_id <> tag_id
    GROUP BY parent_id
) b 

ON a.tag_id = b.parent_id
ORDER BY ParentID

上面的答案返回,

ParentID    ParentName  TotalChildren
3           Tagname-1   2
5           tagname-2   2
6           tagname-3   1
9           tagname-4   1
11          tagname-5   NULL
13          tagname-6   NULL
15          tagname-8   NULL
17          tagname-9   NULL
18          tagname-10  NULL
20          tagname-11  NULL
22          tagname-12  NULL
24          tagname-13  NULL
26          tagname-14  NULL
28          tagname-15  NULL

这是不正确的,因为它返回了所有孩子。

您快到了..只需要将联接作为外部联接:

编辑:

SELECT 
a.tag_id as ParentID,
a.tag_name as ParentName,
b.TotalChildren

FROM root_tags a LEFT OUTER JOIN
(
    SELECT parent_id, COUNT(1) as TotalChildren
    FROM root_tags
    WHERE parent_id <> tag_id
    GROUP BY parent_id
) b 

ON a.tag_id = b.parent_id
WHERE b.TotalChildren is not null
ORDER BY ParentID

我在这里不完全理解您的要求,但是我要说的是,如果将内部b.TotalChildren更改为左b.TotalChildren ,并且将b.TotalChildrenIF(b.TotalChildren is null, 0, b.TotalChildren)那么您将得到您想要的结果集。

SELECT 
a.tag_id as ParentID,
a.tag_name as ParentName,
count(b.child) as TotalChildren
FROM root_tags as a INNER JOIN
(
  SELECT parent_id as child FROM root_tags
  WHERE parent_id is not NULL
)  as b
ON a.tag_id = b.child
where a.parent_id is NULL
ORDER BY ParentID

首选对子查询的联接:

SELECT parents.tag_id AS ParentID,
       parents.tag_name AS ParentName,
       COUNT(childs.tag_id) AS TotalChildren
FROM root_tags AS parents
    LEFT OUTER JOIN root_tags AS childs
        ON parents.tag_id = childs.parent_id
WHERE parents.parent_id IS NULL
GROUP BY parents.tag_id, parents.tag_name
ORDER BY parents.tag_id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM