Your approach with a join and filtering in the where
clause is ok - but you need to correlate the subquery with the outer query so you get the lowest price per product rathern thatn the overall min:
SELECT l.*, p.productname
FROM productscrapeddatalog l
JOIN product p ON l.productID = l.productID
WHERE l.price = (
SELECT MIN(l1.price)
FROM productscrapeddatalog l1
WHERE l1.productID = t.productID
)
If you are running MySQL 8.0, you can also do this with window functions:
SELECT l.*, p.productname
FROM product p
JOIN (
SELECT l.*, RANK() OVER(PARTITION BY productID ORDER BY price) rn
FROM productscrapeddatalog l
) p ON l.productID = l.productID AND l.rn = 1
You should try this:
SELECT productscrapeddatalog.*, product.productname, MIN(productscrapeddatalog.price)
FROM productscrapeddatalog JOIN product
ON productscrapeddatalog.productID = product.productID
ORDER BY productID
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.