Home > Software engineering >  Why doesn't ORDER BY 'id' 'desc' return a syntax error?
Why doesn't ORDER BY 'id' 'desc' return a syntax error?

Time:02-21

I was testing some apparently wrong code and was quite sure it should return a syntax error. But it didn't. The following query works without errors (albeit doesn't sort the table either, which at least meets my expectations):

SELECT * FROM dummy ORDER BY 'id' 'desc';

Interestingly, that

SELECT * FROM dummy ORDER BY id 'desc';

does produce a syntax error.

How does MySQL interpret two strings after ORDER BY? What does it take these strings for? Here is the MCVE

CodePudding user response:

SELECT * FROM dummy ORDER BY 'id' 'desc';

evaluates to

SELECT * FROM dummy ORDER BY 'iddesc';

I.e. ORDER BY a (constant) string literal - which doesn't affect the ORDER BY at all.

Note: This is ANSI/ISO SQL standard, a character literal can be built up by several parts, without having explicit concatenation.

  • Related