GROUP BY HAVING not working for my query

SELECT * FROM `order_offer` WHERE `order_id` = '1131201' GROUP BY `offer_id` HAVING `offer_version` = MAX(offer_version)

What I am trying to do here is pull all of the offers here related to the order ID above, but unique to ONE offer_id with the max offer_version.

Unfortunately, this isn’t working at all. If you have any suggestions, I would greatly appreciate it. I was thinking of just figuring out the solution with php(I am not well versed in sql), but I should probably do it right.

For each order we make an offer per product in the basket. That way we can modify each items pricing and keep the previous record. It is all for records.

Thanks for the help! (:


SELECT * 
FROM order_offer 
INNER JOIN
  (SELECT
       order_id
     , offer_id
     , MAX(offer_version) as maxversion
   FROM order_offer 
   WHERE order_id = '1131201' 
   GROUP BY order_id, offer_id
  ) AS max_offer
WHERE order_offer.order_id = max_offer.order_id
AND   order_offer.offer_id = max_offer.offer_id
AND   order_offer.offer_version = max_offer.maxversion

I changed the last line from

AND   order_offer.offer_version = max_offer.offer_version

to

AND   order_offer.offer_version = max_offer.maxversion

and this worked like a charm. I really thought it was easier than this. Thanks so much for the solution!

Ah yes. I corrected the query in my post.