mysql - Why is my union of two queries not ordering correctly? -
(select `size`, `type` `table` `type`='a' order `size` asc) union (select `size`,`type` `table` `type`='b' order `size` desc)
why isn't query working hope? separates result array types 'a' , types 'b', within type 'a' results, not ordered size (size positive bigint).
any suggestions? :)
the results of query not ordered, unless use order by
clause (or in mysql group by
clause). try want:
(select `size`, `type` `table` `type`='a') union (select `size`,`type` `table` `type`='b') order type, (case when `type` = 'a' size end) asc, (case when `type` = 'b' size end) desc;
ordering clauses on subqueries ignored (the exception when have limit
). and, when not ignored may have not effect on outer query.
actually, forget union all
entirely:
select size, `type` table type in ('a', 'b') order type, (case when `type` = 'a' size end) asc, (case when `type` = 'b' size end) desc;
the first type
in order by
clause unnecessary, think makes intention of ordering clearer.
Comments
Post a Comment