[mysql] arithmetics in ORDER BY

1 view
Skip to first unread message

Jürgen Mukkler

unread,
Mar 6, 2002, 3:39:21 AM3/6/02
to
Hi

I've a problem using arithmetics in an ORDER BY clause:
SELECT a,b FROM foo ORDER BY a-b;
This results in the error:
ERROR 1064: You have an error in your SQL syntax near '-b' at line 1
(a and b are of type int, but I get the same error if I simply
write ORDER BY 1-1)

The version of my mySQL-DB is 3.22.32
On another, slightly newer mySQL-DB (Version 3.23.37) I don't get that error.

Any ideas what's going wrong?

Jim Kennedy

unread,
Mar 6, 2002, 7:13:32 AM3/6/02
to
This works in most databases (not sure about mysql)
select a,b,a-b from foo order by 3
Jim
"Jürgen Mukkler" <juk...@gmx.de> wrote in message
news:a33ae33a.02030...@posting.google.com...

Corra

unread,
Mar 6, 2002, 2:08:16 PM3/6/02
to
Try this query

SELECT a,b,(a-b) as c FROM foo ORDER BY c


"Jürgen Mukkler" <juk...@gmx.de> ha scritto nel messaggio
news:a33ae33a.02030...@posting.google.com...

--CELKO--

unread,
Mar 6, 2002, 5:31:00 PM3/6/02
to
SELECT a,b FROM foo ORDER BY a-b;
This results in the error:
ERROR 1064: You have an error in your SQL syntax near '-b' at line 1
(a and b are of type int, but I get the same error if I simply
write ORDER BY 1-1)

The version of my mySQL-DB is 3.22.32
On another, slightly newer mySQL-DB (Version 3.23.37) I don't get
that error.

>> Any ideas what's going wrong? <<

Standard SQL-92 works this way:

Here is how a SELECT works in SQL ... at least in theory. Real
products will optimize things when they can.

a) Start in the FROM clause and build a working table from all of the
joins, unions, intersections, and whatever other table constructors
are there. The table expression> AS <correlation name> option allows
you give a name to this working table which you then have to use for
the rest of the containing query.

b) Go to the WHERE clause and remove rows that do not pass criteria;
that is, that do not test to TRUE (reject UNKNOWN and FALSE). The
WHERE clause is applied to the working in the FROM clause.

c) Go to the optional GROUP BY clause, make groups and reduce each
group to a single row, replacing the original working table with the
new grouped table. The rows of a grouped table must be group
characteristics: (1) a grouping column (2) a statistic about the group
(i.e. aggregate functions) (3) a function or (4) an expression made up
of the those three items.

d) Go to the optional HAVING clause and apply it against the grouped
working table; if there was no GROUP BY clause, treat the entire table
as one group.

e) Go to the SELECT clause and construct the expressions in the list.
This means that the scalar subqueries, function calls and expressions
in the SELECT are done after all the other clauses are done. The AS
operator can give a name to expressions in the SELECT list, too.
These new names come into existence all at once, but after the WHERE
clause, GROUP BY clause and HAVING clause has been executed; you
cannot use them in the SELECT list or the WHERE clause for that
reason.

If there is a SELECT DISTINCT, then redundant duplicate rows are
removed. For purposes of defining a duplicate row, NULLs are treated
as matching (just like in the GROUP BY).

f) Nested query expressions follow the usual scoping rules you would
expect from a block structured language like C, Pascal, Algol, etc.
Namely, the innermost queries can reference columns and tables in the
queries in which they are contained.

g) ORDER BY is not part of the SELECT statement; it would make no
sense to sort a set. It is part of a cursor which gets the completed
rsult set and turns it into a sequential file structure in the host
language. That means the ORDER BY can only use the column names that
appear in the SELECT list -- you have to use an alias or real column
name, not a positional number!! Positional numbers were deprecated in
SQL-92.

Christopher Browne

unread,
Mar 6, 2002, 8:33:32 PM3/6/02
to
Centuries ago, Nostradamus foresaw when 71062...@compuserve.com (--CELKO--) would write:
> SELECT a,b FROM foo ORDER BY a-b;
> This results in the error:
> ERROR 1064: You have an error in your SQL syntax near '-b' at line 1
> (a and b are of type int, but I get the same error if I simply
> write ORDER BY 1-1)
>
> The version of my mySQL-DB is 3.22.32
> On another, slightly newer mySQL-DB (Version 3.23.37) I don't get
> that error.
>
>>> Any ideas what's going wrong? <<
>
> Standard SQL-92 works this way:
>
> Here is how a SELECT works in SQL ... at least in theory. Real
> products will optimize things when they can.

A somewhat quicker answer would be to simply say that standard SQL
doesn't want to order by _expressions_, but rather by _column names_.

A query that would "do the trick," albeit introducing an extra column
to the result, would be:

SELECT a, b, a-b as c FROM foo ORDER BY c;

Conceivably, you might get "the right result" without the extra column
from:
SELECT a, b from
SELECT a, b, a-b as c FROM foo ORDER BY c;

Although that has two problems:
a) MySQL doesn't support subselects. (It's a pretty weak SQL
implementation, in some ways...)

b) It would probably be "fair game" for an implementation to
throw away the "order by" since it's not in the main select.
--
(reverse (concatenate 'string "moc.adanac@" "enworbbc"))
http://www.ntlug.org/~cbbrowne/linuxxian.html
"No matter how much money you spend, you can't make a racehorse out of
a pig. You can, however, make an awfully fast pig."
-- An old saying about program efficiency

Lee Fesperman

unread,
Mar 6, 2002, 8:59:10 PM3/6/02
to
--CELKO-- wrote:
> g) ORDER BY is not part of the SELECT statement; it would make no
> sense to sort a set. It is part of a cursor which gets the completed
> rsult set and turns it into a sequential file structure in the host
> language. That means the ORDER BY can only use the column names that
> appear in the SELECT list -- you have to use an alias or real column
> name, not a positional number!! Positional numbers were deprecated in
> SQL-92.

That's news to me! What section of the SQL-92 says that? Section 13.1 specifies that
positional numbers are allowed in the ORDER BY of a cursor.

--
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)

Reply all
Reply to author
Forward
0 new messages