问题描述
我有下表称为“产品”:
id INT 11
product_name VARCHAR 255
category VARCHAR 255
subcategory VARCHAR 255
subsubcategory VARCHAR 255
我想在一个查询中读取所有类别,子类别和子子类别(每个类别一次)。
目前,我有以下查询,但是我不确定它是否有效:
SELECT category,subcategory,subsubcategory FROM products WHERE category != '' GROUP by category,subsubcategory ORDER by category ASC,subcategory ASC,subsubcategory ASC
输出应类似于:
clothes (main category)
clothes > pants (sub category)
clothes > pants > man (sub sub category)
clothes > pants > woman
clothes > skirt
jewelry
jewelry > necklace
解决方法
您可以这样做:
select category as rendered from products
union all
select concat(category,' > ',subcategory) from products
union all
select concat(category,subcategory,subsubcategory) from products
order by rendered