luv2code
luv2code

Reputation: 1296

Only getting one result from sub select in inner join in mysql

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!

Upvotes: 0

Views: 167

Answers (1)

Taryn
Taryn

Reputation: 247840

You are missing the GROUP BY in your subquery, so MySQL will only return one-value. If you want all hotel_id's then you need to GROUP BY that field:

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

Upvotes: 1

Related Questions