first I would like to say that I'm aware of the only_full_group_by restriction and I understand why it's behaving this way. And I would prefer have a fine query that desactivating this option.
SELECT campaigns.uuid, campaigns.name, coupons.name, coupons.value, coupons.product_id, coupons.uuid FROM `campaigns`
LEFT JOIN coupons ON coupons.uuid = campaigns.coupon_id
GROUP BY coupons.product_id
ORDER BY coupons.value DESC
In my coupons table I can have several coupons related to the same product_id, but I would like to return only one coupon by product, the one with the higher value.
I'm scratching my head from 4 hours but I can't find out to do it.
Thank you for your help.
Here is the structure of coupons table (useless fields removed) :
- uuid
- name
- value
- product_id
Where I can have 3 differents row :
- uuid1 - name1 - 1.5 - 1
- uuid2 - name2 - 0.80 - 2
- uuid3 - name3 - 0.90 - 1
What I would like to return is just :
- uuid1 - name1 - 1.5 - 1
- uuid2 - name2 - 0.80 - 2
And skip the name 3 since it's the same product ID and I only want to return the coupon with the highest value.
In fact I also have other table joins to retrieve some data but I skipped them from the example to make it clearer.
Hope it explains better what I mean.