I have two tables that I need to combine to get the data I need, but the queries that give the desired result are all very expensive.
I have a products table (id, name) and a stock table (id, pid, supplier, stock, price). The stock.pid is the foreign key to products.id, but each product can have multiple suppliers and therefor multiple entries in the stock table.
What I need is for each product the cheapest price and current stock, combined with all data from the products record, ordered on prices ascending.
What I tried is (and several variations):
SELECT DISTINCT(pid), MIN(price), stock, p.*
FROM stock s LEFT (INNER) JOIN
product
ON pid = s.id
GROUP BY pid
ORDER BY price (LIMIT 100)
Um
maybe