在内部查询中使用外部查询中的列(相关子查询?)

时间:2018-12-06 18:27:22

标签: mysql sql correlated-subquery

我想要的是每种产品,根据产品的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;

0 个答案:

没有答案