I am using php and Mysql and need help to display UNIQUE results for a price comparison.
My tables are:
tblItems
ID | SKU | Shop | Name | Size
1 S1 Shop1 A S
2 S2 Shop1 B M
3 S3 Shop1 C L
4 S4 Shop2 A S
tblProductFeed
ID | SKU | Shop | Price
1 S1 Shop1 12
2 S2 Shop1 14
3 S3 Shop1 15
4 S4 Shop2 11
The idea is a price comparison.
I want to compare all records where Size='S'. But I only want to get the lowest price from tblProductFeed, and I only want one record where the price is lowest.
My thoughts are like:
Get SKU, Shop and Name from tblItems where Size='S'
Compare matching rows in tblProductFeed where SKU and Shop are matching
Get the lowest price from tblProductFeed
Display only one DISTINCT Name from TblItems and with the lowest price
Results:
Name | Price
A 11
B 14
C 15
Maybe my idea is bad, please correct me.
Before I decided to scrap data and build to tables my sql was like this, might help to get some inspiration.
SELECT tbl.*
FROM tblProductFeed tbl
INNER JOIN (SELECT Size
, MIN(Price) MinPrice
FROM tblProductFeed
WHERE Size = '". $str ."'
GROUP BY Size) tbl1
ON tbl1.Size = tbl.Size
WHERE tbl1.MinPrice = tbl.Price;

