我有這個查詢(注意,所有3個子查詢都從同一行中選擇不同的列)。本質上,我想獲得product_bid
中的行,最大日期與product
行一起。MySQL加入子查詢結果
SELECT
p.product_id,
p.product_title,
(SELECT b.bid_id FROM product_bids b WHERE b.bid_product_id=p.product_id ORDER BY b.bid_date DESC LIMIT 1) AS bid_id,
(SELECT b.bid_date FROM product_bids b WHERE b.bid_product_id=p.product_id ORDER BY b.bid_date DESC LIMIT 1) AS bid_date,
(SELECT b.bid_amount FROM product_bids b WHERE b.bid_product_id=p.product_id ORDER BY b.bid_date DESC LIMIT 1) AS bid_amount
FROM product p
WHERE p.product_auction_id=325
有沒有辦法做一次子查詢得到的product_bids
的PK,並就加入(子查詢的結果),或這樣做的任何干淨的方式?
注意事項:查詢優化器是否能識別這個問題,使它不那麼重要?
感謝
這是完美的,只是我的原始查詢也退回的產品在'product_bids'沒有相應的行。其中的一個可以是「左連接」嗎? – Adam
@亞當:是的,絕對的(雖然我已經命令他們,但實際上你會想'RIGHT JOIN product ...')。 – eggyal
剛發佈時就已經想出了它。謝謝。完善。 – Adam