dongsuyou6938 2014-02-03 23:02
浏览 51
已采纳

MySQL努力寻求查询以查找产品之间的类似细节

I have a query to write and I am absolutely stumped on how to do it. Here's my situation, I am trying to provide a particular product_ID, then match all of the other product_IDs in the database that have at least the same intDescription_detail_IDs as the provided product_ID.

The relevant tables look like this:

tblproducts

=========================

product_ID | product_name

=========================

| 1    |    dresser  |
| 2    |    bookcase | 
| 3    |    table    |
| 4    |    chair    |

=========================

tbldescriptions

=========================================================================

|description_ID| intDescription_product_ID | intDescription_detail_ID   |

=========================================================================

|   1      |        1      |        1       |
|   2      |        1      |        2       |
|   4      |        2      |        1       |
|   5      |        2      |        2       |
|   6      |        2      |        6       |
|   7      |        3      |        1       |
|   8      |        3      |        3       |
|   9      |        3      |        4       |
|   10     |        4      |        1       |
|   11     |        4      |        2       |
|   12     |        4      |        7       |

As an example, if I provided the product_ID "1", then I would like to return all of the product_IDs that at least have intDescription_detail_ID 1 and 2.

So, the product_IDs that should be returned are 1, 2, and 4, because all of these products have the intDescription_detail_ID of 1 and 2 among their details.

I am highly confused about how to write a query like this, so any help is greatly appreciated!

  • 写回答

2条回答 默认 最新

  • dongningwen1146 2014-02-04 00:02
    关注

    Alternative to @Strawberry’s suggestion with JOINs this can also be done using HAVING for filtering products that have (at least) the same number of rows with the same intDescription_detail_IDs as the product the search is done for:

    SELECT intDescription_product_ID
    FROM tbldescriptions t1
    WHERE intDescription_detail_ID IN (
      SELECT intDescription_detail_ID
      FROM tbldescriptions t2
      WHERE t2.intDescription_product_ID = 1
      )
    GROUP BY intDescription_product_ID
    HAVING count(*) >= (
      SELECT count(intDescription_detail_ID)
      FROM tbldescriptions t3
      WHERE t3.intDescription_product_ID = 1
      )
    

    http://sqlfiddle.com/#!2/ce698/2

    One should keep in mind though that HAVING is applied last, so that will select all products with at least one matching intDescription_detail_ID first, and filter the results based on the actual count afterwards – so depending on the size and characteristic of your data set that might not be the best performing solution.

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测