Monday, 2 September 2013

mysql conditional query inner join

mysql conditional query inner join

I have the following tables: quotes quote_items old_products new_products
quotes.id = quote_items.quote_id
old_products.id = quote_items.old_product_id
new_products.id = quote_items.new_product_id
I have this query which works out the average % saved:
Select ROUND((sum(q.`old_kwh`) - sum(`p`.`new_kwh`))/sum(q.`old_kwh`)*100) as
percentage_saved from quotes
inner join quote_items
on quotes.id = `quote_items`.quote_id
INNER JOIN `new_products` p
ON p.id = new_product_id
inner join old_products q
ON q.id = old_product_id
The problem is this: for certain old_products, 'old_kwh' = 0. In that case
I need:
quote_items_input.input value, where quote_items_input.quote_item_id =
quote_items.id.
quote_items_input being another table and input value being the field.
How can I add this to the above query? I was looking at CASE conditions
with mysql, but have had no luck so far.
Any help would be awesome Thanks

No comments:

Post a Comment