Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

mysql - Join two tables and extract the lowest price with multiple conditions (multiple sku)

I almost finished my system until I noticed that a product had a different sku with another supplier (I have more than 15,000 products in the table table_prices from 7 different suppliers, it is assumed that they all have to handle the same SKU (but they don't), I came up with adding sku2 to solve, but I got into a black hole.

Now taking advantage of the fact that I can get help from you, I would like to be able to search in the table table_prices for the lowest price if the sku, sku2, upc or ean matches table_products, in this way I will not have a major problem if a supplier decides to put a bad sku since I will have more information to relate.

I leave a link with what I have working ... I look for sku of table_products and I get the lowest price of table_prices

http://sqlfiddle.com/#!9/47809f/1/0

I hope to get $90 on product 1

SELECT p.*, x.supplier AS supplier, x.price , x.quantity AS quantity 
FROM table_prices x 
JOIN 
    (SELECT sku, 
    MIN(price) price 
    FROM table_prices 
    WHERE quantity != 0 AND active = 1 
    GROUP BY sku) y 
 ON y.sku = x.sku 
AND y.price = x.price 
JOIN table_products p 
WHERE p.sku = x.sku 
ORDER BY category, price
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This seems to give the desired results:

SELECT p.*, x.supplier AS supplier, x.price , x.quantity AS quantity 
FROM table_prices x 
JOIN table_products p ON (p.sku = x.sku OR p.sku2 = x.sku OR p.upc = x.upc OR p.ean = x.ean)
WHERE x.price = (SELECT MIN(x2.price) FROM table_prices x2 WHERE (p.sku = x2.sku OR p.sku2 = x2.sku OR p.upc = x2.upc OR p.ean = x2.ean))
ORDER BY category, price 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...