简体   繁体   English

mysql select 类别 e 来自同一表的子类别

[英]mysql select categories e subcategories from the same table

I have the following table called "products":我有下表称为“产品”:

id INT 11
product_name VARCHAR 255
category VARCHAR 255
subcategory VARCHAR 255
subsubcategory VARCHAR 255

I would like to read all categories, sub-categories and sub-sub-categories (only once each category) in one single query.我想在一个查询中阅读所有类别、子类别和子子类别(每个类别仅一次)。

For now I have the following query, but im not sure it's working:现在我有以下查询,但我不确定它是否有效:

SELECT category, subcategory, subsubcategory FROM products WHERE category != '' GROUP by category, subcategory, subsubcategory ORDER by category ASC, subcategory ASC, subsubcategory ASC

The output should be something like: output 应该类似于:

clothes (main category)
clothes > pants (sub category)
clothes > pants > man (sub sub category)
clothes > pants > woman
clothes > skirt
jewelry
jewelry > necklace

You can do:你可以做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM