我想要的是每种产品,根据产品的department_id获取该产品的url。递归查询有效,问题所在的行基本上是
select id, title, parent_id from departments where id = prod.department_id
我收到错误ERROR 1054 (42S22): Unknown column 'prod.department_id' in 'where clause'
。
如果我用4这样的常量替换这一行中的prod.department_id,那么整个事情就起作用了,只有我为每个产品获得相同的网址。
我无法访问子查询中的每个产品的列,我该怎么做?我在OS X上使用mysql 8.0.12,这是完整的查询:
select id, title, short_description,
(base_price+base_price*tax) as retail_price,
lower(concat('www.someurl.com/',
(with recursive cte (id, title, parent_id) as (
select id, title, parent_id from departments where id = prod.department_id
union all (
select p.id, p.title, p.parent_id
from departments p inner join cte on cte.parent_id = p.id
)
) select
GROUP_CONCAT(lower(title) order by id SEPARATOR '/') as path from cte
))) as url
from products prod
inner join featured_products on featured_products.product_id = prod.id;