合并来自不同查询的结果

Combine results from different queries

我有一个简化的 table 设置如下:

Table 1(产品)

product_id    product_parent_id
646           45

Table 2(类别)

product_id    category_name
45            category_1

Table 3 (product_names)

product_id     slug
45             product45-details
646            product646-details

For product_id 646 我想获取 product_id 45 的类别和产品 646 的 slug 并将它们组合成一个输出。

所以基本上结果应该是:

   id  link
   646 category_1/product646-details-detail

到目前为止我有以下内容:

select product_id, CONCAT('I.category_name,'/',E.slug,'-detail') as link from products C left join product_names E on C.product_parent_id=E.product_id left join category H on E.product_id=H.product_id where C.product_id > 1 group by C.product_id

根据 product_id 而不是 parent_parent_id

获得 E.slug 结果的方法是什么

你可以join:

select p.product_id, c.category_name, pn.slug
from products p
inner join category c on c.product_id = p.product_parent_id
inner join product_names pn on pn.product_id = p.product_id
where p.product_id = 646