When you fetch rows from MySQL, they are returned to you in a haphazard order that might or might not fit your needs. Thankfully, MySQL allows you to specify in which order the rows should be returned. The order is specified using the ORDER BY clause. The clause accepts a list of one or more expressions (which can just be column names). For every row, the expressions in the ORDER BY clause are evaluated. Then the resulting values are put into a list that is then sorted. The rows are then returned according to this order. Sound complex? Here is a simplified version of the steps laid out for you:
|