doubu1853 2015-11-04 14:07
浏览 102
已采纳

MySQL选择Product-SimilarProduct

I have two tables products and similar_products. Products tables holds data related to available products however this tables has loads of products that are same but with different product id. Which is why I have another table that hold the data for all the similar products. I need to run a query that selects products from products table but at the same also check in similar_products tables to not select any duplicate products.

So for example:

Products Table:

ProductID | Manufacturer  | Part No     | Name
-----------------------------------------------------------------
8202      | Hp            | 402146-B21  | HP Auto Synch Cable
8210      | Hp            | 113894-B21  | HP Stylus 3 Pack
8211      | Hp            | 113894-B21  | HP Stylus 3 Pack
8212      | Hp            | 113894-B21  | HP Stylus 3 Pack

Similar_products Table

ProductID | Similar_ProductID
----------|-------------------
8210      | 8211
8210      | 8212
8211      | 8210
8211      | 8212
8212      | 8210
8212      | 8211

How can I run a query that will only select ProductID 8202 and 8210 and not select duplicates products.

  • 写回答

1条回答 默认 最新

  • dqoys62082 2015-11-04 14:29
    关注

    In case you always have the relationship x~y as the two records [x,y] and [y,x] in Similar_products your problem is a variation of The Rows Holding the Group-wise Maximum of a Certain Column

    SELECT
        p.ProductID, p.Name
    FROM
        Products as p
    WHERE
        NOT EXISTS(
            SELECT
                1
            FROM
                Similar_products s
            WHERE
                p.ProductID=s.ProductID
                AND p.ProductID>s.Similar_ProductID
        )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害