0

Hi I have run into a dilemma, I am doing this query:

SELECT GROUP_CONCAT(DISTINCT(ca.category_name) SEPARATOR ', ') AS categories, pr.promo_name, pr.add_value_text, c.contract_id, c.cmeal_plan, c.cmin_markup, c.civa, c.tax_include, c.hotel_id, hi.hname, hi.hstars, im.image_file, pl.plan_name, ra.price
FROM contracts AS c
INNER JOIN hotel_info AS hi ON hi.hotel_id = c.hotel_id AND hi.destination_id = '6460'
INNER JOIN images AS im ON im.foreign_id = hi.hotel_id
INNER JOIN meal_plan AS pl ON pl.plan_code = c.cmeal_plan AND pl.lang = '1'
INNER JOIN hotel_categories AS hc ON hc.hotel_id = hi.hotel_id
INNER JOIN categories AS ca ON ca.category_code = hc.category_code AND ca.lang = '1'
LEFT JOIN 
                    (SELECT 
                              r.hotel_id, AVG(r.double) AS price
                    FROM
                              rates AS r ) AS ra
                    ON ra.hotel_id = hi.hotel_id
LEFT JOIN promotions AS pr ON pr.hotel_id = hi.hotel_id AND FIND_IN_SET(c.contract_id, pr.contract_id) > 0 AND pr.book_start <= '2012-11-01' AND pr.book_end >= '2012-11-02' AND travel_start <= '2012-11-23' AND travel_end >=  '2012-11-30' AND pr.lang = '1'
WHERE c.cstart <= '2012-11-01' AND c.cend >= '2012-11-01'
AND hi.status = '1'
AND im.type ='1'
GROUP BY hi.hotel_id

I am getting all the desired results except for the sub select query.. each hotel has a price but it is only giving me back one result and the rest are all null. Is there an error in my query? If any additional information is needed please let me know and thank you in advance for any help!

4

1 回答 1

1

您的子查询中缺少GROUP BY,因此 MySQL 只会返回一个值。如果您想要所有hotel_id,那么您需要GROUP BY该字段:

SELECT GROUP_CONCAT(DISTINCT(ca.category_name) SEPARATOR ', ') AS categories, 
    pr.promo_name, 
    pr.add_value_text, 
    c.contract_id, 
    c.cmeal_plan, 
    c.cmin_markup, 
    c.civa, 
    c.tax_include, 
    c.hotel_id, 
    hi.hname, 
    hi.hstars, 
    im.image_file, 
    pl.plan_name, 
    ra.price
FROM contracts AS c
INNER JOIN hotel_info AS hi 
    ON hi.hotel_id = c.hotel_id 
    AND hi.destination_id = '6460'
INNER JOIN images AS im 
    ON im.foreign_id = hi.hotel_id
INNER JOIN meal_plan AS pl 
    ON pl.plan_code = c.cmeal_plan 
    AND pl.lang = '1'
INNER JOIN hotel_categories AS hc 
    ON hc.hotel_id = hi.hotel_id
INNER JOIN categories AS ca 
    ON ca.category_code = hc.category_code 
    AND ca.lang = '1'
LEFT JOIN 
(
    SELECT r.hotel_id, AVG(r.double) AS price
    FROM rates AS r 
    GROUP BY r.hotel_id  <-- add a GROUP BY hotel_id then you will get avg() for each hotel
) AS ra
    ON ra.hotel_id = hi.hotel_id
LEFT JOIN promotions AS pr 
    ON pr.hotel_id = hi.hotel_id 
    AND FIND_IN_SET(c.contract_id, pr.contract_id) > 0 
    AND pr.book_start <= '2012-11-01' 
    AND pr.book_end >= '2012-11-02' 
    AND travel_start <= '2012-11-23' 
    AND travel_end >=  '2012-11-30' 
    AND pr.lang = '1'
WHERE c.cstart <= '2012-11-01' 
    AND c.cend >= '2012-11-01'
    AND hi.status = '1'
    AND im.type ='1'
GROUP BY hi.hotel_id
于 2012-11-01T21:22:02.663 回答