Sorting Search Results with ORDER BY


The next clause in the SELECT statement is ORDER BY . This clause allows us to sort the result rows on one or more columns . The sort can be either ascending , denoted ASC , or descending, denoted DESC . For example:

 
 select * from employee order by job asc, name desc; 

This will select all the rows and columns from the employee table. They will be sorted according to job in alphabetical order, and if two or more people have the same job, they will be sorted in reverse alphabetical order by name. This will give the following results:

 
 +-------------+---------------+-----------------------+---------------+  employeeID   name           job                    departmentID   +-------------+---------------+-----------------------+---------------+         9842  Ben Smith      DBA                               42          7513  Nora Edwards   Programmer                       128          6651  Ajay Patel     Programmer                       128          9006  Candy Burnett  Systems Administrator            128  +-------------+---------------+-----------------------+---------------+ 4 rows in set (0.02 sec) 

If you just specify ORDER BY column with no ASC or DESC , the default is ASC . Note that if ORDER BY is not specified, you can't assume anything about the order in which rows will be returned.



MySQL Tutorial
MySQL Tutorial
ISBN: 0672325845
EAN: 2147483647
Year: 2003
Pages: 261

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