php - mysql 从同一个表中选择类别 e 子类别
问题描述
我有下表称为“产品”:
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, subcategory, 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
推荐阅读
- python - 如何创建一个 if 语句来通知用户选择的索引高于元组中的最大索引?
- java - 我可以在 Spring Security 中使用多个配置来保护 Web 应用程序和 Rest API 吗?
- java - Apache PDFBox:有没有办法只使用流?
- python - 从文本数据中提取特定数字
- oracle - 如何使用单个控制文件将常见的平面文件数据加载到两个不同的表中?
- r - 将函数输出添加到 data.table 作为新列而不命名它们
- python - 熊猫将字符串转换为列表以使用爆炸
- go - 在 Go 中查找句子中的单词列表
- c# - 在 Entity Framework Core 中保存一对多关系
- c++ - Qt Widget C++ 自动调整为更小的屏幕(见屏幕截图)