【问题标题】:Combine results from different queries组合来自不同查询的结果
【发布时间】:2020-12-14 22:22:02
【问题描述】:

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

表 1(产品)

product_id    product_parent_id
646           45

表2(类别)

product_id    category_name
45            category_1

表 3(产品名称)

product_id     slug
45             product45-details
646            product646-details

对于 product_id 646,我想获取 product_id 45 的类别和 product 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 结果的方法是什么

【问题讨论】:

    标签: mysql sql subquery inner-join


    【解决方案1】:

    你可以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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-24
      • 2021-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 2023-02-04
      • 1970-01-01
      相关资源
      最近更新 更多