Sorting Query Results


Whereas the WHERE conditional places restrictions on what records are returned, the ORDER BY clause will affect how those records are presented. Much as listing the columns of a table arranges the returned order (compare Figures 4.12 and 4.14), ORDER BY structures the entire list. When you do not dictate the order of the returned data, it will be presented to you in somewhat unpredictable ways (although probably on the primary key in ascending order).

 SELECT * FROM tablename ORDER BY column SELECT email FROM users ORDER BY  registration_date 

The default order when using ORDER BY is ascending (abbreviated ASC), meaning that numbers increase from small to large and dates go from older to most recent. You can reverse this order by specifying DESC.

 SELECT email FROM users ORDER BY  registration_date DESC 

You can even order the returned values by multiple columns, as I'll show in the following example.

To sort data

1.

Select all of the users in alphabetical order by last name (Figure 4.21).

 SELECT first_name, last_name FROM  users ORDER BY last_name; 

Figure 4.21. The records in alphabetical order by last name.


If you compare these results with those in Figure 4.12, you'll see the benefits of using ORDER BY.

2.

Display all of the users in alphabetical order by last name and then first name (Figure 4.22).

 SELECT first_name, last_name FROM  users ORDER BY last_name ASC,  first_name ASC; 

Figure 4.22. The records in alphabetical order, first by last name, and then by first name within that.


In this query, the effect would be that every row is returned, first ordered by the last_name, and then by first_name within the last_names. The effect is most evident among the Simpsons.

3.

Show all of the users by date registered (Figure 4.23).

 SELECT * FROM users ORDER BY  registration_date DESC; 

Figure 4.23. All of the users displayed by date registered, with the most recent listed first.


You can use an ORDER BY on any column type, including numbers and dates.

Tips

  • As part of the nature of databases, the order records are stored in a table is inconsequential. To give significance to the order records are returned in, use an ORDER BY on a column.

  • Because MySQL works naturally with any number of languages, the ORDER BY will be based upon the language being used by the database (English as a default).

  • If the column that you choose to sort on contains NULL values, those will appear first, both in ascending and descending order.

  • You can, and frequently will, use ORDER BY with WHERE or other clauses. When doing so, place the ORDER BY after the other conditions:

     SELECT * FROM users WHERE  registration_date >= '2005-03-01  ORDER BY last_name ASC 




    PHP and MySQL for Dynamic Web Sites. Visual QuickPro Guide
    PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    ISBN: 0321336577
    EAN: 2147483647
    Year: 2005
    Pages: 166
    Authors: Larry Ullman

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net