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