What does the ORDER BY число construct in a MySQL query mean?

Normally, ORDER BY used in conjunction with the name of the column by which you want to sort the resulting rowset, and what does it mean when a number is used instead of a column name?

  • the ordinal number of the column in the sample - Mike

2 answers 2

ORDER BY число allows you to sort by column number. This syntax is marked as not recommended.

    ORDER BY число uses the ordinal column number from the SELECT row when sorting.

    It is not recommended to use queries sorted by column numbers. This is due to the fact that over time, the structure of the table may change, for example, as a result of adding / deleting columns. As a result, the query may give a completely different sequence or even cause an error, referring to the missing column.

    More on here and here .

    • 6
      Justification is delusional. Don't write SELECT * . And if in the request to list specific fields, we are no longer dependent on the modification of the table. In addition, ORDER BY for calculated fields should be used only by number, otherwise we will calculate twice - Anton Shchyrov
    • @AntonShchyrov, if the calculation is absolutely the same in select and in order by, wouldn't he guess to calculate once? - Qwertiy
    • @Qwertiy With a fright? And if any calculating-logging function is used? Or something like Random() ? - Anton Shchyrov
    • one
      @Anton Shchyrov, Order by is executed after SELECT. Therefore, it is possible to use both the alias of the calculated column and its number. - msi
    • one
      @Denis. Once again, carefully re-read my phrase " You should not write SELECT * . And if you list specific fields in the query , then we are no longer dependent on the table modification - Anton Shchyrov