好的,我对查询的操作感到困惑,但是我想做的是以下操作:获取按product_id分组的最低出价,然后加载与该出价相关的产品信息。
当前,当运行下面的查询时,它告诉我bid_id(其中product_id = 2列为30,但绝对不是30)应为120(尽管bid_price值正确为29.99):
SELECT lowbid.bid_id, lowbid.bid_price FROM (SELECT bid_id, min(bid_price) AS bid_price, product_id FROM tbl_products_bid WHERE is_active = 1 AND is_deleted = 0 GROUP BY product_id) AS lowbid;
现在由于这个查询给了我随机的bid_id,我不知道为什么我想知道一个SQL专家是否可以为我提供一个洞察力:1.我是否完全胖了; 2.我是否有另一种方式,或者为什么我可以获得与该bid_price无关的随机bid_id。
我创建了一个SQLFiddle,它可以解释我的意思,但是任何帮助将不胜感激。
http://sqlfiddle.com/#!2/de77b/14
也只是为了让您知道此查询是另一个查询的一部分,但我删除了我认为给我带来问题的元素(即上面的内容),下面是较大的查询的一部分:
SELECT lowestbid.bid_id, lowestbid.product_id, lowestbid.bid_price as seller_bid_price, seller_description, pb.is_countdown, pb.startdate, pb.enddate FROM tbl_products_bid pb inner JOIN ( SELECT bid_id, product_id, min(bid_price) as bid_price, seller_id, description as seller_description, is_countdown, startdate, enddate from tbl_products_bid where is_active = 1 group by product_id ) AS lowestbid ON pb.bid_id = lowestbid.bid_id order by lowestbid.bid_price asc
SELECT a.* FROM tbl_products_bid a INNER JOIN ( SELECT product_id, MIN(bid_price) min_price FROM tbl_products_bid GROUP BY product_id ) b ON a.product_id = b.product_id AND a.bid_price = b.min_price