Welcome Guest, Not a member yet? Register   Sign In
Price database and confusing SQL query
#11

[eluser]Relexx[/eluser]
[quote author="MarcL" date="1234927082"]Sorry for my ignorance but I've been having a look at ActiveRecord and I'm not sure that I can do the inner query with it. Is that right?[/quote]

you could always create a view for this instead

Code:
create view last_prices as select max(created_on) as created, product_id, merchant_id from prices

Then join the view

Code:
select prc.id, prc.price, prd.id, prd.name, mer.name
from prices prc, product prd, merchant mer, last_prices lp
// join the data from the subquery back to the prices table so that you select the max record
// and thus the most recent price
where prc.product_id = lp.product_id
and   prc.merchant_id = lp.merchant_id
and   prc.created_on = lp.created
// then add the joins for the product and merchant tables
and   prd.id = prc.product_id
and   mer.id = prc.merchant_id


Messages In This Thread
Price database and confusing SQL query - by El Forum - 02-17-2009, 05:13 AM
Price database and confusing SQL query - by El Forum - 02-17-2009, 05:55 AM
Price database and confusing SQL query - by El Forum - 02-17-2009, 06:04 AM
Price database and confusing SQL query - by El Forum - 02-17-2009, 06:43 AM
Price database and confusing SQL query - by El Forum - 02-17-2009, 07:39 AM
Price database and confusing SQL query - by El Forum - 02-17-2009, 10:00 AM
Price database and confusing SQL query - by El Forum - 02-17-2009, 01:39 PM
Price database and confusing SQL query - by El Forum - 02-17-2009, 03:06 PM
Price database and confusing SQL query - by El Forum - 02-17-2009, 03:18 PM
Price database and confusing SQL query - by El Forum - 02-17-2009, 03:40 PM
Price database and confusing SQL query - by El Forum - 02-17-2009, 11:14 PM
Price database and confusing SQL query - by El Forum - 02-18-2009, 03:10 AM
Price database and confusing SQL query - by El Forum - 02-18-2009, 06:59 PM



Theme © iAndrew 2016 - Forum software by © MyBB