Mutually exclusive values in SQL

限于喜欢 提交于 2021-02-10 05:40:57

问题


I have a query which selects products from a table. A product can have multiple prices (think of various prices) and a default price.

Naturally, this is a one-to-many relation. I need to select the products which have a given price, or the default price - which means mutual exclusion. I know this can be done through separate queries and a WHERE (not) IN clauses or a union statement, but I'm convinced a more optimal way must be possible. My query currently looks like this:

SELECT products.*, products_prices.price 
FROM products RIGHT JOIN 
     products_prices ON (products.id = products_prices.productId) 
WHERE products_prices.businessId = ? 
OR    products_prices.businessId IS NULL // this needs to become mutual.

EDIT: I ended up using this query, which is a slightly modified version of Gordon Linoff's:

 SELECT distinct p.*, coalesce(pp.price, defpp.price)
 FROM products p LEFT JOIN 
      products_prices pp
      ON p.id = pp.productId and pp.businessId = ? left join
      products_prices defpp
      on p.id = defpp.productId and defpp.businessId is NULL

回答1:


If I understand your question correctly, the products table would have the default price and the product_prices table would have any other price.

You want to know where the default price is being used, meaning that there are no other prices. For this, use a left outer join:

SELECT p.*, coalesce(pp.price, p.default_price)
FROM products p LEFT OUTER JOIN 
     products_prices pp
     ON p.id = pp.productId
WHERE pp.price = GIVENPRICE or pp.price is null

Based on your comment, you are storing the default prices in records with the business id being NULL. In this case, I would do two joins to the prices table:

SELECT p.*, coalesce(pp.price, defpp.price)
FROM products p LEFT OUTER JOIN 
     products_prices pp
     ON p.id = pp.productId and pp.price = GIVENPRICE left outer join
     products_prices defpp
     on p.id = defpp.productId and defpp.businessId is NULL

The first join gets the price matching the given price. The second gets the default price. The first result is used, if present, otherwise the second is used.



来源:https://stackoverflow.com/questions/15526050/mutually-exclusive-values-in-sql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!