将LEFT OUTER JOIN重写为子查询

时间:2015-09-27 15:42:49

标签: sql subquery outer-join

是否可以重写以下左连接:

SELECT wanted.id AS wanted_id, offers.id AS offer_id, departure_city,
departure_country, destination_city, destination_country, departure_date_min, departure_date_max, MIN(price) as price
FROM wanted LEFT JOIN (SELECT id, wanted_id, SUM(price) AS price FROM `offers` GROUP BY offer_date) AS offers ON wanted.id = offers.wanted_id
WHERE wanted.id IN (SELECT wanted_id FROM wanted_by_user WHERE user_ID=%d) group by offer_id

作为子查询? 我已经尝试过这样,从技术上讲,它工作正常,但是:它只能像内部连接一样工作(如果表中没有匹配,则表示不显示行'提供'):

FROM wanted, (SELECT id, SUM(price) AS price FROM `offers` GROUP BY offer_date) AS offers "
    ."WHERE wanted.id IN(SELECT wanted_id FROM wanted_by_user WHERE user_ID=%s)

0 个答案:

没有答案