我想在分組之前對結果進行排序,但似乎無法完成。MySQl GROUP BY和ORDER BY不做我想做的事
我有這個疑問:
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
結果如下表:
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
如果我按product_number,color_number我得到ID 和371
但我想要得到編號和371,因爲我想在按照size_position對它們進行分組之前先對結果進行排序。
我真的不知道如何做到這一點?!?
有人可以幫我嗎?
謝謝!
你能解釋一下結果應該是什麼樣子嗎? – Kwebble 2010-08-12 11:58:35
謝謝大家,我用php循環訪問'未分組'數據解決了這個問題。這是真正的禁食方式。再次感謝! – Johan 2010-08-16 12:27:04