具有连接语句

时间:2017-02-28 11:27:23

标签: mysql join

我有以下表格

1)折扣

+--------------------------------------------+
| ID   discount_description   discount_type  |
+--------------------------------------------+
| 17   20% off                PERCENT        |
| 19   Citric                 ABSOLUTE       |
+--------------------------------------------+

表2 - 包含的产品

+------+--------------+------------------+
|  ID  | discount_id  |    product_id    |
+------+--------------+------------------+
|   2  |          17  |         52238403 |
|   3  |          17  |         52238409 |
|   4  |          19  |         52238408 |
+------+--------------+------------------+

表3 - 排除的产品

+---------------------------------+---------------+
| ID   discount_id                |  product_id   |
+---------------------------------+---------------+
| 2    17                         |      52238411 |
| 3    17                         |      52238408 |
+---------------------------------+---------------+

我需要查询根据包含的表和排除表中的产品ID获取所有折扣。它还应包括折扣行,这些行不包括在产品包含/排除表中。

以下是根据产品52238408,52238403获取折扣的示例。

    SELECT 
        discounts.id as ID1,
        discount_products.product_id as p1,
        exclude_discount_products.product_id as p2 
    FROM discounts

    LEFT JOIN `discount_products` ON 1=1 
        AND discounts.id = discount_products.discount_id

    LEFT JOIN `exclude_discount_products` ON 1=1 
        AND discounts.id = exclude_discount_products.discount_id

    WHERE discount_products.product_id IN (52238408,52238403) 
        AND exclude_discount_products.product_id NOT IN (52238408,52238403)

查询只是一个基本版本,我很确定它是不正确的。但基本上我想检查两个表的产品ID。

我认为因为产品被排除在52238408之后得到折扣19作为输出,所以应该忽略17。但它反而给出了17。不确定我错过了什么,非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:1)

不确定你是否想要这个你没有包括预期的输出。

创建表/插入数据

CREATE TABLE discounts
    (`ID` INT, `discount_description` VARCHAR(7), `discount_type` VARCHAR(8))
;

INSERT INTO discounts
    (`ID`, `discount_description`, `discount_type`)
VALUES
    (17, '20% off', 'PERCENT'),
    (19, 'Citric', 'ABSOLUTE')
;

CREATE TABLE discount_products
    (`ID` INT, `discount_id` INT, `product_id` INT)
;

INSERT INTO discount_products
    (`ID`, `discount_id`, `product_id`)
VALUES
    (2, 17, 52238403),
    (3, 17, 52238409),
    (4, 19, 52238408)
;


CREATE TABLE exclude_discount_products
    (`ID` INT, `discount_id` INT, `product_id` INT)
;

INSERT INTO exclude_discount_products
    (`ID`, `discount_id`, `product_id`)
VALUES
    (2, 17, 52238411),
    (3, 17, 52238408)
;

<强>查询

SELECT 
   discounts.id AS ID1
 , product_id AS p1
 , ( 
      # SELECT matching product_id FROM exclude_discount_products based on product_id (checks excludes)

      SELECT
        product_id
      FROM
        exclude_discount_products
      WHERE
        product_id = discount_products_NOT_IN_exclude_discount_products.product_id
    )
     AS p2
FROM ( 
  # SELECT all discount_products that dont have the same discount_id, product_id as in exclude_discount_products 
  SELECT 
     *
  FROM 
   discount_products
  WHERE (
      discount_id
    , product_id
  ) 
  NOT IN (
    SELECT 
        discount_id
      , product_id
    FROM 
      exclude_discount_products  
  )  
)
 AS 
  discount_products_NOT_IN_exclude_discount_products

INNER JOIN 
 discounts
ON
  discount_products_NOT_IN_exclude_discount_products.discount_id = discounts.id   

WHERE
 product_id IN(
    52238408
  , 52238403 
)

<强>结果

   ID1        p1        p2  
------  --------  ----------
    17  52238403      (NULL)
    19  52238408    52238408