sql - mysql - order by inside subquery -
sql - mysql - order by inside subquery -
i used next query mysql 5.5 (or previous versions) years without problems:
select t2.code (select country.code country order country.code desc ) t2;
the order of result descending needed.
last week, migrated new mysql version (in fact, migrated mariadb 10.0.14) , same query same database not sorted descending anymore. sorted ascending (or sorted using natural order, not sure in fact).
so, can tell me if bug or if alter of behaviour in recent versions of mysql/mariadb?
thank you.
g. plante
after bit of digging, can confirm both scenarios:
mysql 5.1 apply order by
within subquery.
mariadb 5.5.39 on linux not apply order by
within subquery when no limit
supplied. does correctly apply order when corresponding limit
given:
select t2.code ( select country.code country order country.code desc limit 2 ) t2;
without limit
, there isn't reason apply sort within subquery. can equivalently applied outer query.
as turns out, mariadb has documented behavior , not regarded bug:
a "table" (and subquery in from
clause too) - according sql standard - unordered set of rows. rows in table (or in subquery in from
clause) not come in specific order. that's why optimizer can ignore order by
clause have specified. in fact, sql standard not allow order by
clause appear in subquery (we allow it, because order ... limit
... changes result, set of rows, not order).
you need treat subquery in from
clause, set of rows in unspecified , undefined order, , set order by
on top-level select
.
so mariadb recommends applying order by
in outermost query, or limit
if necessary.
note: don't have access proper mysql 5.5 or 5.6 confirm if behavior same there (and sqlfiddle.com malfunctioning). comments on original bug report (closed not-a-bug) suggest mysql 5.6 behaves same way mariadb.
mysql sql subquery sql-order-by mariadb
Comments
Post a Comment