I have a query in which I want to order the join table before joining it.
It's something like this:
Each product may have 1 or more subproducts with varying prices. I want to select a product and the lowest price from the list of sub-products.
Intuitively speaking, I would put the "order by" on the join. But clearly this doesn't work! I have also tried using a sub-query like this:
This works but is taking about 8 seconds for the query to run (about 4000 rows returned). When I remove the sub-query, this drops to under 1 second.
Can anyone point me in the right direction?
Thanks
Henry
It's something like this:
Code:
SELECT product, price FROM prods INNER JOIN subprods ON prodPk=prodFk ORDER BY price ASC GROUP BY prodPk
Intuitively speaking, I would put the "order by" on the join. But clearly this doesn't work! I have also tried using a sub-query like this:
Code:
SELECT product, (SELECT price FROM subprods WHERE prodFk=prodPK ORDER BY price ASC LIMIT 0,1) AS price FROM prods
Can anyone point me in the right direction?
Thanks
Henry
Comment