MySQL
  Home arrow MySQL arrow Page 4 - Speaking SQL (part 2)
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
MYSQL

Speaking SQL (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2001-01-18


    Table of Contents:
  • Speaking SQL (part 2)
  • Christmas Presents
  • Teacher's Pet
  • Reading Backwards
  • Count() Me In
  • Like, You Know, Man...
  • Joining Them Together
  • Nest Egg

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Speaking SQL (part 2) - Reading Backwards
    ( Page 4 of 8 )

    If you'd like to see the data from your table ordered by a specific field, SQL offers the ORDER BY construct. This construct allows you to specify both the column name and the direction in which you would like to see data (ascending or descending).

    For example, if you'd like to see data from the "members" table above arranged by id, you could try this:

    mysql> SELECT * FROM members ORDER BY member_id; +-----------+-------+---------+---------+----------------------------+ | member_id | fname | lname | tel | email | +-----------+-------+---------+---------+----------------------------+ | 1 | John | Doe | 1234567 | jdoe@somewhere.com | | 2 | Jane | Doe | 8373728 | jane@site.com | | 3 | Steve | Klingon | 7449373 | steve@alien-race.com | | 4 | Santa | Claus | 9999999 | santa@the-north-pole.com | | +-----------+-------+---------+---------+----------------------------+ 4 rows in set (0.06 sec)
    And you could reverse the order with

    mysql> SELECT * FROM members ORDER BY member_id DESC; +-----------+-------+---------+---------+----------------------------+ | member_id | fname | lname | tel | email | +-----------+-------+---------+---------+----------------------------+ | 4 | Santa | Claus | 9999999 | santa@the-north-pole.com | | | 3 | Steve | Klingon | 7449373 | steve@alien-race.com | | 2 | Jane | Doe | 8373728 | jane@site.com | | 1 | John | Doe | 1234567 | jdoe@somewhere.com | +-----------+-------+---------+---------+----------------------------+ 4 rows in set (0.00 sec)
    You can limit the number of records in the result set with the LIMIT keyword - this keyword takes two parameters, which specify the row to start with and the number of rows to display. So the query

    SELECT * FROM videos LIMIT 2,2;
    would return rows 3 and 4 from the result set.

    mysql> SELECT * FROM videos LIMIT 2,2; +----------+------------------+--------------+ | video_id | title | director | +----------+------------------+--------------+ | 3 | Charlie's Angels | McG | | 4 | Any Given Sunday | Oliver Stone | +----------+------------------+--------------+ 2 rows in set (0.00 sec)
    You can combine the ORDER BY and LIMIT constructs to quickly get the four newest records in the table - as the following example demonstrates;

    mysql> SELECT * FROM videos ORDER BY video_id DESC LIMIT 0, 4; +----------+------------------+----------------+ | video_id | title | director | +----------+------------------+----------------+ | 6 | Woman On Top | Fina Torres | | 5 | Hollow Man | Paul Verhoeven | | 4 | Any Given Sunday | Oliver Stone | | 3 | Charlie's Angels | McG | +----------+------------------+----------------+ 4 rows in set (0.00 sec)


    This article copyright Melonfire 2001. All rights reserved.

     
     
    >>> More MySQL Articles          >>> More By icarus, (c) Melonfire
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek