Home arrow MySQL arrow Page 4 - Speaking SQL (part 2)

Reading Backwards - MySQL

After learning how to insert and edit data in a database, thesecond part of our SQL tutorial takes an in-depth look at the SELECTquery,and explains how to use joins, sub-queries and built-in functions tofocus in on the data you need.

TABLE OF CONTENTS:
  1. Speaking SQL (part 2)
  2. Christmas Presents
  3. Teacher's Pet
  4. Reading Backwards
  5. Count() Me In
  6. Like, You Know, Man...
  7. Joining Them Together
  8. Nest Egg
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 5
January 18, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
   

MYSQL ARTICLES

- Xeround Releases Free Version of MySQL Cloud...
- Oracle Announces New MySQL Specialization
- Constant Contact Chooses SkySQL for MySQL Su...
- Revoke Statement in MySQL
- The Grant Statement in MySQL
- SuccessBricks Announces ClearDB Availability...
- Building a PHP ORM: Deploying a Blog
- TROSYS Launches Free MySQL Manager and Admin...
- Building an ORM in PHP: Domain Modeling
- Building an ORM in PHP
- MySQL Leads Open Source Market, Gets Cluster...
- Oracle Announces Milestone Release for MySQL
- How to Stop SQL Injection Attacks
- New Defragmentation Solution for SQL Server
- Comparison of MyISAM and InnoDB MySQL Databa...


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap

Dev Shed Tutorial Topics: