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

7bsow1i6  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(206)

我想要的是,对于每个产品,根据产品的部门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\u id,整个过程都会正常工作,只是每个产品的url都相同。
我无法访问子查询中每个产品的列,我该怎么做?我在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;

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题