在自联接查询上使用COUNT

时间:2013-05-29 21:06:48

标签: mysql

我有一个填充双选列表的查询。该查询通过自联接从同一个表中检索主要类别和子类别。基本上它连接自己使用id和parent id获取每个类别中的子类别。我曾尝试使用COUNT来获取子类别的数量,以便我可以将数字添加到第一个列表的输出中。我没有编写查询,我只想重构它来计算每个main中的subs。

SELECT 
    root.name AS root_name, 
    root.link AS root_link, 
    root.RETAIL AS root_RETAIL, 
    root.WHOLESALE AS root_WHOLESALE,
    root.linkto AS root_linkto, 
    root.keywords AS root_keywords, 
    root.description AS root_description, 
    root.id AS root_id,
    down1.name AS down1_name,
    down1.linkto AS down1_linkto, 
    down1.link AS down1_link, 
    down1.keywords AS down1_keywords,
    down1.description AS down1_description, 
    down1.id AS down1_id, 
    down1.parentid AS down1_parentid, 
    down1.RETAIL AS down1_RETAIL, 
    down1.WHOLESALE AS down1_WHOLESALE
FROM categories AS root
        LEFT OUTER JOIN categories AS down1 ON down1.parentid = root.id
WHERE root.parentid = 0
ORDER BY root_name, down1_name 

这将返回填充两个列表的所有内容。我尝试用down1_name围绕down1_idCOUNT,但它会计算所有内容 - 而不是每个主要内部的潜艇数量。我不确定使用其他自联接来执行此操作,还是使用嵌套SELECT COUNT。任何见解都会非常感激。 TKS。

3 个答案:

答案 0 :(得分:2)

尝试使用子查询

SELECT r.name AS r_name, 
       r.link AS r_link, 
       r.RETAIL AS r_RETAIL, 
       r.WHOLESALE AS r_WHOLESALE,
       r.linkto AS r_linkto, 
       r.keywords AS r_keywords, 
       r.description AS r_description, 
       r.id AS r_id,
       d.name AS d_name,
       d.linkto AS d_linkto, 
       d.link AS d_link, 
       d.keywords AS d_keywords,
       d.description AS d_description, 
       d.id AS d_id, 
       d.parentid AS d_parentid, 
       d.RETAIL AS d_RETAIL, 
       d.WHOLESALE AS d_WHOLESALE,
       (SELECT COUNT(*) FROM categories 
         WHERE parentid = r.id) d_count
  FROM categories AS r LEFT OUTER JOIN categories AS d 
    ON d.parentid = r.id
 WHERE r.parentid = 0
 ORDER BY r_name, d_name

<强> SQLFiddle

答案 1 :(得分:1)

如果你尝试这样的事情吗?

select count(cs.down1_name) as mycount from
    (
    SELECT root.name AS root_name, 
    root.link AS root_link, 
    root.RETAIL AS root_RETAIL, 
    root.WHOLESALE AS root_WHOLESALE,
    root.linkto AS root_linkto, 
    root.keywords AS root_keywords, 
    root.description AS root_description, 
    root.id AS root_id,
    down1.name AS down1_name,
    down1.linkto AS down1_linkto, 
    down1.link AS down1_link, 
    down1.keywords AS down1_keywords,
    down1.description AS down1_description, 
    down1.id AS down1_id, 
    down1.parentid AS down1_parentid, 
    down1.RETAIL AS down1_RETAIL, 
    down1.WHOLESALE AS down1_WHOLESALE
    FROM categories AS root
    LEFT OUTER JOIN categories AS down1 ON down1.parentid = root.id
    WHERE root.parentid = 0
    ORDER BY root_name, down1_name ) as cs
    group by cs.down1_name

----------------- UPDATE ----------------------------- --------

这样的东西?:

SELECT cat_a.name, 
   cat_a.link, 
   cat_a.RETAIL, 
   cat_a.WHOLESALE,
   cat_a.linkto, 
   cat_a.keywords, 
   cat_a.description, 
   cat_a.id,
   cat_b.name cat_b_name,
   cat_b.linkto cat_b_linkto, 
   cat_b.link cat_b_link, 
   cat_b.keywords cat_b_keywords,
   cat_b.description cat_b_description, 
   cat_b.id cat_b_id, 
   cat_b.parentid cat_b_parentid, 
   cat_b.RETAIL cat_b_RETAIL, 
   cat_b.WHOLESALE cat_b_WHOLESALE,
   (SELECT COUNT(1) FROM categories 
     WHERE parentid = cat_a.id) as cat_b_count
FROM categories cat_a LEFT OUTER JOIN categories cat_b 
ON (cat_b.parentid = cat_a.id)
WHERE cat_a.parentid = 0

这是小提琴:http://sqlfiddle.com/#!2/4de04c/11

答案 2 :(得分:0)

查询查询可能会更容易。像这样的东西(我们假设主查询名为q1)。

<cfquery name="q2" dbtype="query">
select r_id maincat
, count(*) subcategories
from q1
where d_name is not null
group by r_id
</cfquery>

<cfquery name="q3" dbtype="query">
select q1.*
, subcategories
from q1, q2
where r_id = maincat
</cfquery>

然后使用q3填充列表而不是q1。