I've looked on Google but couldn't find it. Can we order in MySQL using field's position ?
Let's say I got fields ID | FirstName | LastName | Age
but I want to order by field 4, could I do something like ORDER BY FieldPos4
to order on Age ? I have not manage to find something about that.
Thanks.
We can also specify column position in Order by clause. In this query, column birthdate is at the 3rd position; therefore, we can use three in the Order by clause to sort results on this column data.
The ORDER BY statement in SQL is used to sort the fetched data in either ascending or descending according to one or more columns. By default ORDER BY sorts the data in ascending order. We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order.
Yes, you can order by a field(s)even if it is not your in your select statement but exists in your table.
mysql> desc reOrderColumn; The following is the output. Now re-order the column using ALTER MODIFY command. I will reorder the DeliveryDate column after the ProductName column.
You can refer to the columns by position (1-based):
ORDER BY 4
From the documentation (emphasis mine):
Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using column names, column aliases, or column positions. Column positions are integers and begin with 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With