选择最具体的连接结果

时间:2013-07-11 08:25:47

标签: mysql sql join

我正在尝试让mysql返回商店项目的最具体折扣,因为可能会有几个折扣。我的表和数据如下(不相关的列已被省略):

Item
  itemId   brand    supplier   price
  ======   =====    ========   =====
  Item1    Brand1   Supply1    100
  Item2    Brand2   Supply1    100
  Item3    Brand3   Supply1    100
  Item4    Brand4   Supply2    100

Discount
  discountId   itemId   brand    supplier   discountPrice
  ==========   ======   ======   ========   =============
  Discount1    (null)   (null)   Supply1    80
  Discount2    (null)   Brand2   Supply1    60
  Discount3    Item3    (null)   (null)     40

我预期的查询输出将是

itemId  price  discountPrice
===================================
Item1   100    80
Item2   100    60
Item3   100    40
Item4   100    (null)

你可以看到我的规则是

  1. 供应商折扣是最不具体的
  2. 供应商+品牌折扣更具体化
  3. ItemId折扣是最具体的
  4. 与子句或在子句上的正常左连接将返回所有组合,而不是最具体的折扣。我怎样才能做到这一点?

    select item.itemId, item.price, discount.discountPrice from item left join discount on (item.itemId = discount.itemId) or (item.brand = discount.brand and item.supplier = discount.supplier) or (item.supplier = discount.supplier AND discount.brand IS NULL)

2 个答案:

答案 0 :(得分:1)

查询:

<强> SQLFIDDLEEXample

SELECT i.itemId, 
       i.price, 
       COALESCE(d.discountPrice, d2.discountPrice, d3.discountPrice) AS discountPrice 
FROM item i
LEFT JOIN discount d 
  ON i.itemId = d.itemId
LEFT JOIN discount d2
  ON i.brand = d2.brand
  AND i.supplier = d2.supplier 
LEFT JOIN discount d3
 ON i.supplier = d3.supplier 
 AND d3.brand IS NULL

结果:

| ITEMID | PRICE | DISCOUNTPRICE |
----------------------------------
|  Item1 |   100 |            80 |
|  Item2 |   100 |            60 |
|  Item3 |   100 |            40 |
|  Item4 |   100 |        (null) |

答案 1 :(得分:0)

这是我的方式: 对所有三个折扣使用单独的左连接,并从中选择最具体的

Select 
    i.itemId, 
    i.price,
    coalesce(spec3.discountPrice, spec2.discountPrice, spec1.discountPrice)
from item i
left join Discount spec3 on (i.itemId = spec3.itemId)
left join Discount spec2 on (i.supplier = spec2.supplier and i.brand = spec2.brand)
left join Discount spec1 on (i.supplier = spec1.supplier)

上面的查询可能包含一些语法错误,我附近没有实际运行它的mysql服务器。