Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't ORDER BY 'id' 'desc' return a syntax error?

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

like image 556
Your Common Sense Avatar asked Oct 24 '25 16:10

Your Common Sense


1 Answers

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. (Almost correct, a new-line is required as separator.)

like image 98
jarlh Avatar answered Oct 26 '25 06:10

jarlh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!