Why is this MySQL query correct?

I have a table with three columns. When I print the following query

select * from MyTable order by 5 and 2; 

I get everything in the table (the result is equal to select * from MyTable; ). It was originally expected that I would get some kind of error. But I did not understand why?

+6
source share
4 answers

What happens here is that 5 and 2 treated as an expression that evaluates to 1. However, it should not produce a result sorted by the first column.

Actually, I think that you only get sorted data, because you inserted them into a sorted sequence. Take a look at this SQLFiddle:

http://sqlfiddle.com/#!2/3e04e/1

Data is not sorted by any of the columns , sorted by value 1 .

+2
source

order by 5 and 2 interpreted as order by (5 and 2) , which is a constant expression, so no real ordering is performed, and the data is simply displayed in the order in which it was inserted.

+6
source

5 and 2 are column indices, and they mean the 5th column and the 2nd column.

+1
source
 select * from MyTable order by 5,2; It means start with index 5 and bring 2record ie 5,6,7 
0
source

Source: https://habr.com/ru/post/957341/


All Articles