好的,我对我的查询发生了什么感到困惑,但我想做的是:获取按 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_id 甚至与该bid_price 无关。
我创建了一个 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