0

我有一个填充双选列表的查询。该查询通过自连接从同一个表中检索主要类别和子类别。基本上,它使用 id 和父 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但它计算所有内容 - 而不是每个主要中的潜艇数量。我不确定是否使用另一个自连接来执行此操作或嵌套SELECTwith COUNT。任何见解将不胜感激。谢了。

4

3 回答 3

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

于 2013-05-29T21:13:58.310 回答
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

- - - - - - - - -更新 - - - - - - - - - - - - - - - - -----

像这样的东西?:

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

于 2013-05-29T21:11:54.413 回答
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 填充您的列表。

于 2013-05-29T21:45:38.483 回答