【问题标题】:MYSQL filter results after inner join and whereMYSQL 内连接后过滤结果和 where
【发布时间】:2021-05-25 13:59:35
【问题描述】:

我通过内部连接有一个 mysql 查询:

SELECT * FROM oc_product
INNER JOIN oc_product_to_category ON oc_product.product_id = oc_product_to_category.product_id 
WHERE oc_product_to_category.category_id = 100

到目前为止,它返回表 oc_product 中对应于 oc_product_to_category.category_id = 100 的行

我想从这些结果中选择表 oc_product 中的常用行 其中 oc_product_to_category.category_id = 101 或 oc_product_to_category.category_id = 102 等等...

我怎样才能做到这一点?

我尝试通过以下方式:

SELECT * FROM oc_product
INNER JOIN oc_product_to_category ON oc_product.product_id = oc_product_to_category.product_id 
WHERE oc_product_to_category.category_id = 101 OR oc_product_to_category.category_id = 102
having (oc_product_to_category.category_id = 301)

但结果错误。

谁能指出我正确的方向(逻辑)?

【问题讨论】:

    标签: mysql


    【解决方案1】:

    您通过通用产品 ID 加入是一回事,这只是直接条件。要包含多个类别,您只需使用“IN”子句并提供您感兴趣的不同类别的列表。

    SELECT 
          * 
       FROM 
          oc_product p
             INNER JOIN oc_product_to_category pc
                ON p.product_id = pc.product_id 
                AND pc.category_id IN ( 101, 102, 301 )
    

    现在,您的问题不清楚,但是如果您试图暗示您正在寻找属于 A 类但也与 B 类相关联的产品,并且希望确保这两个类别都被涵盖。以计算机为例。你卖电脑。有人对笔记本电脑(A 类)感兴趣,但也对 i7 处理器(B 类)和 15 英寸屏幕(C 类)感兴趣。您要确保所有 3 个感兴趣的类别都在结果中。您不需要 13 英寸笔记本电脑,也不需要 i7 台式机/塔式机。如果这种情况更多,您可能希望对所需的 3 个类别进行汇总计数。在这种情况下,我会从满足所有 3 个要求的产品/类别表中进行预查询聚合,然后获得符合条件的产品。类似的东西

    select
          p.*
       from
          ( select
                  -- I only care about the final product ID that qualifies
                  pc.product_id
               from
                  oc_product_to_category pc
               where
                  -- the product category must be of these 3 possible choices
                  pc.category_id in ( 101, 102, 301 )
               group by
                  pc.product_id
               having
                      -- first SUM() qualifies that the 301 MUST BE included
                      -- hence its = 1 value.
                      sum( case when pc.category_id = 301 
                                then 1 else 0 end ) = 1
                  AND 
                      -- AND the sum() of the others is GREATER than 0.
                      -- so either OR both would qualify the product
                      sum( case when pc.category_id in ( 101, 102 )
                                then 1 else 0 end ) > 0  ) JustQualified
             -- NOW, joining to the products based on only those that qualified above
             JOIN oc_product p
                on JustQualified.product_id = p.product_id
    

    相关更新

    要对上述查询中我作为别名“p”的 OC_Product 表执行相关更新查询,只需对语法稍作更改

    update oc_product p
       JOIN ( select
                      -- I only care about the final product ID that qualifies
                      pc.product_id
                   from
                      oc_product_to_category pc
                   where
                      -- the product category must be of these 3 possible choices
                      pc.category_id in ( 101, 102, 301 )
                   group by
                      pc.product_id
                   having
                          -- first SUM() qualifies that the 301 MUST BE included
                          -- hence its = 1 value.
                          sum( case when pc.category_id = 301 
                                    then 1 else 0 end ) = 1
                      AND 
                          -- AND the sum() of the others is GREATER than 0.
                          -- so either OR both would qualify the product
                          sum( case when pc.category_id in ( 101, 102 )
                                    then 1 else 0 end ) > 0  ) JustQualified
                 -- NOW, joining to the products based on only those that qualified above
          on JustQualified.product_id = p.product_id
       set p.myColumn = 22
    

    【讨论】:

    • 感谢您的回答,但是这两种情况都不涵盖我的预期结果。我想首先得到 - 过滤 oc_product_to_category.category_id = 301 的产品,然后从中找到满足 oc_product_to_category.category_id = 101 或 oc_product_to_category.category_id = 102 的产品。
    • @qwertyg,为您修改了第二个查询。
    • 优秀的答案这就是我想要的。最后一个问题,( 101, 102, 301 ) 中 pc.category_id 行中的 id 顺序是否起作用?
    • @qwertyg,不,“IN”子句不关心序列,如果您愿意,我看到人们发布了一个带有 100 个 ID 的 IN 子句。不是长列表上最好的方法,但可能。很高兴它有帮助。
    • @qwertyg,查看相关更新查询的修订答案
    猜你喜欢
    • 2023-03-19
    • 2012-05-23
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多