The only way order by would make sense in a " substatement" is if you combine it with limit: use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result. To apply limit to the the last " substatement" and also to the entire union statement, use:īut note that applying order by to " substatement"s is meaningless because the docs have explicitly stated that order by is only guaranteed ( cf.) to work when applied to the entire union statement: If you want to apply limit to the last " substatement" (as opposed to the entire union statement), you must surround the last " substatement" with braces: We've mentioned previously that the limit keyword can also be applied to individual " substatement"s: These two keywords apply to the entire union statement, not just the last " substatement": We can see that the entire union statement ends with an optional order by / limit. (Which means they are no longer optional.) If you'd like to use order by, the " substatement" that contains order by must be surrounded by braces. Next, each " substatement" can contain where, group by, having, join, limit, but not order by. Mysql> (select 1) union all select 2 union all (select 3) - error because not all "substatement"s are braced Mysql> (select 1) union all (select 2) union all select 3 - error because not all "substatement"s are braced ![]()
0 Comments
Leave a Reply. |