0

我有这个查询:

SELECT DISTINCT b.name AS NAME, p.photo AS photo, MIN(p.price) AS minprice, COUNT(p.id) AS productcount
FROM blocks b, typesproducts tp, products p
WHERE (tp.typeid = b.typeid OR tp.typeid IN (SELECT id FROM types WHERE parent = b.typeid)) 
AND tp.productid = p.id AND p.active = 1 AND b.active = 1
AND p.id IN (SELECT id FROM products WHERE active = 1 ORDER BY price))

它工作得很好。

唯一的问题是照片包含产品表中的随机照片。

我想得到的是与最便宜的产品匹配的照片,即与获取 MIN(p.price) 列的行匹配的产品。

任何人都知道如何更新查询,以便我得到正确的照片?

非常感谢。

4

1 回答 1

0

我可能是错的,但看起来你正在寻找这样的东西

SELECT *
  FROM products 
 WHERE price = (SELECT MIN(price) FROM products WHERE active = 1)

SQLFiddle

于 2013-05-01T03:31:56.540 回答