I want to sort my results before grouping them, but I can't seem to get it done.
I have this query:
SELECT DISTINCT product.id, product_number_value.att_value AS product_number, color_number_value.option_code AS color_number, size_value.option_code AS size_code, size_value.option_position AS size_position FROM product INNER JOIN product_attribute_relational AS product_number_relational ON product_number_relational.product_id = product.id INNER JOIN product_att_varchar AS product_number_value ON product_number_relational.product_att_id = product_number_value.product_att_id INNER JOIN product_attribute_relational AS color_number_relational ON color_number_relational.product_id = product.id INNER JOIN product_att_select AS color_number_select ON color_number_relational.product_att_id = color_number_select.product_att_id INNER JOIN attribute_option_value AS color_number_value ON color_number_select.att_value = color_number_value.option_id INNER JOIN product_attribute_relational AS size_relational ON product.id = size_relational.product_id INNER JOIN product_att_select AS size_select ON size_relational.product_att_id = size_select.product_att_id INNER JOIN attribute_option_value AS size_value ON size_select.att_value = size_value.option_id WHERE product_number_relational.att_id = 1 AND color_number_relational.att_id = 2 AND size_relational.att_id = 3 AND product.id IN (365, 366, 367, 368, 369, 371, 372, 373, 374) ORDER BY product.id ASC
The result is the following table:
id product_number color_number size_code size_position 365 F23740 311 S 24 366 F23740 311 M 25 367 F23740 311 L 26 368 F23740 311 XL 27 369 F23740 311 XS 23 371 F23745 213 S 24 372 F23745 213 M 25 373 F23745 213 L 26 374 F23745 213 XL 27
If I group by product_number, color_number I get id 365 and 371.
But I want to get id 369 and 371, because I want the results to first order by size_position, before grouping them.
I really have no idea on how to get this done?!??
Can someone help me please??
Thanks!