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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog