Re: 1--1

From: Tony Mountifield <tony_at_mountifield.org>
Date: Wed, 21 Jan 2015 18:01:08 +0000 (UTC)
Message-ID: <m9oph4$jdf$1_at_softins.softins.co.uk>


In article <05863e1a-f5f9-49b5-8305-b1cd3ccbffbb_at_googlegroups.com>, Kiuhnm Mnhuik <gandalf23_at_mail.com> wrote:
> I don't understand why this works as expected:
> select *
> from games
> order by 2
> but this doesn't:
> select *
> from games
> order by 1--1
> The "order by" is completely ignored.

It depends on your value of "expected". It helps to mention that!

The first query orders by the value of the second selected column (whatever that may be - rather opaque when you use "select *").

The second query evaluates the expression 1--1 to the constant 2, and then as Lew said, uses that constant as the sort key for all records, which gives no sorting at all. So it could appear as if the ORDER BY is ignored, and in fact the optimiser might indeed choose to ignore it if it is clever enough to notice.

Selection of columns by position can only be done using literal column numbers, not expressions.

Cheers
Tony

-- 
Tony Mountifield
Work: tony_at_softins.co.uk - http://www.softins.co.uk
Play: tony_at_mountifield.org - http://tony.mountifield.org
Received on Wed Jan 21 2015 - 19:01:08 CET

Original text of this message