drq231358 2014-02-24 07:03
浏览 8
已采纳

查询返回基于类别的重复产品

I have a mysql query that joins 5 different tables to return specific properties of products. It should return each product only one time. But since the product may have multiple categories it's returning the products once for each category.

Here's the select:

SELECT DISTINCT 
       pd.name AS pname, 
       p.price AS price, 
       cd.name AS cname, 
       pd.description AS pdescription, 
       pd.language_id AS language_id, 
       pd.product_id AS product_id, 
       p.model AS model, 
       p.sku AS sku, 
       p.upc AS upc, 
       m.name AS brand 
  FROM {$this->prefix}product_description pd 
       LEFT JOIN {$this->prefix}product_to_category pc 
                 ON (pd.product_id = pc.product_id) 
       INNER JOIN {$this->prefix}product p 
                 ON (pd.product_id = p.product_id) 
       LEFT JOIN {$this->prefix}category_description cd 
                 ON (cd.category_id = pc.category_id 
                     AND cd.language_id = pd.language_id) 
       LEFT JOIN {$this->prefix}manufacturer m 
                 ON (m.manufacturer_id = p.manufacturer_id)

If a given product, say product_id 32 is assigned to more than one category, it will return product_id 32 once for each category, the only difference being the result cname being the category name.

If someone could help rework the select to only include each product once, no matter the category I would appreciate the help.

It would also be helpful to have the category returned to be the MAX() category_id for the product, but not a huge deal if that's too much to ask.

Thank you.

  • 写回答

2条回答 默认 最新

  • dtypj3308 2014-02-24 07:09
    关注

    If you use GROUP BY, you can get each product once irrespective of the category:

    SELECT DISTINCT 
    pd.name AS pname, 
    p.price AS price, 
    cd.name AS cname, 
    pd.description AS pdescription, 
    pd.language_id AS language_id, 
    pd.product_id AS product_id, 
    p.model AS model, 
    p.sku AS sku, 
    p.upc AS upc, 
    m.name AS brand 
    FROM {$this->prefix}product_description pd 
    LEFT JOIN {$this->prefix}product_to_category pc 
    ON (pd.product_id = pc.product_id) 
    INNER JOIN {$this->prefix}product p 
    ON (pd.product_id = p.product_id) 
    LEFT JOIN {$this->prefix}category_description cd 
    ON (cd.category_id = pc.category_id AND cd.language_id = pd.language_id) 
    LEFT JOIN {$this->prefix}manufacturer m 
    ON (m.manufacturer_id = p.manufacturer_id)
    GROUP BY product_id;
    

    For getting the MAX() category, you can try and apply this MAX function on the column that you want. I guess in this case it would be MAX(cd.name) AS cname.

    For more info on GROUP BY you can go through the following manual page:

    https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建