好吧,我对我的问题感到困惑,但我想做的是:
获取按产品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;
现在由于这个查询给了我随机的出价id,我不确定为什么我想知道一个SQL专家是否能为我提供1的洞察力。如果我是完全厚或2。如果有其他的方法或者为什么我可以得到随机出价,甚至与出价无关。
我已经创建了一个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
SQLFiddle Demo