Mysql
 sql >> Database >  >> RDS >> Mysql

sql query:come fare in modo che i tag senza figli diventino genitori?

Ci sei quasi.. devi solo rendere il join uno esterno:

MODIFICATO:

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