问题
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