【发布时间】:2023-04-04 05:09:01
【问题描述】:
我想对两个表“产品”和“类别”运行查询,我的产品表中有 30 条记录。 product 表有一个列名 category_ids varchar(255),它存储 products 表中每条记录的 category ids,格式为(10,11,12,130,..)。在排序中,一个产品可以是多个类别。具有列名 parent_id 的类别表,它是该类别的父类别。
我想列出该产品的所有类别的记录。
例如,查看 id = 7 的 products 表的一条记录。
product_Id = 7,
category_ids = '213,215,216',
product_name = 'Ponds',
.....
表示产品池有三个 category = category.id = 213、category.id = 215 和 category.id = 216。
我想在这里列出所有三个池塘的记录,就像这种格式:=
product_Id | product_name | category_name | parent_category_name
7 ponds cream chemical
7 ponds medicine chemical
7 ponds powder Ayurvedic
我正在尝试这个查询:-
select
p.id as product_id,
p.product_name,
child.name as category_name,
parent.name as parent_category_name
from category child
left join products p on child.id in(p.category_ids)
left join category parent on parent.id = child.parentid and parent.parentid = 0
where p.id = 7
上面的查询只得到一条记录,而不是上面的所有三条记录。
这个查询中的什么条件和加入将被应用到上面描述的结果中。
很抱歉浪费了您宝贵的时间。
任何建议和想法将不胜感激。
非常感谢。
【问题讨论】: