SQL,获取特定的数据而不仅仅是随机数据

时间:2013-04-30 23:30:14

标签: sql

我有这个问题:

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)列的行匹配的产品。

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

非常感谢。

1 个答案:

答案 0 :(得分:0)

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

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

<强> SQLFiddle