I have a problem with tables joining.
I have two tables: product_commons and product_prices. Product_prices has records with standard product prices + pre-generated prices for campaigns (campaign_id). I need a query which returns me list of products with campaign prices (if exists) or standard prices (if they not).
Something like this:
SELECT * FROM product_commons
INNER JOIN product_prices ON product_commons.id = product_prices.product_id
WHERE (campaign_id = 3 OR campaign_id IS NULL)
GROUP BY product_commons.id
Unfortunnely this query returns me prices only with campaign_id = NULL.
You need to use a mix of INNER and LEFT (OUTER) JOINs with a COALESCE, with the campaign_id filter in the JOIN conditions. To get 2 prices from different rows into one row, you need 2 JOINs.
You don’t need a GROUP BY either: it doesn’t make sense (and would give errors used like this) in standard SQL or other RDBMS