MySQL
  Home arrow MySQL arrow Page 2 - Speaking SQL (part 2)
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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? 
MYSQL

Speaking SQL (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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) - Christmas Presents


    (Page 2 of 8 )

    Throughout this article, I'll be using the database I developed last time to better illustrate the various examples. Just to refresh your memory, here's what it looked like:

    # members table +-----------+-------+---------+---------+--------------------------+ | 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 | +-----------+-------+---------+---------+--------------------------+ # videos table +----------+-------------------------------+------------------+ | video_id | title | director | +----------+-------------------------------+------------------+ | 1 | Star Wars: The Phantom Menace | George Lucas | | 2 | ET | Steven Spielberg | | 3 | Charlie's Angels | McG | | 4 | Any Given Sunday | Oliver Stone | | 5 | Hollow Man | Paul Verhoeven | | 6 | Woman On Top | Fina Torres | +----------+-------------------------------+------------------+ # status table +-----------+----------+ | member_id | video_id | +-----------+----------+ | 2 | 6 | | 4 | 2 | | 1 | 1 | | 1 | 2 | | 1 | 3 | +-----------+----------+
    The simplest form of the SELECT query is the "catch-all" query, which returns all the records in a specific table. It looks like this:

    mysql> SELECT * FROM members; +-----------+-------+---------+---------+----------------------------+ | 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.00 sec)
    The asterisk (*) indicates that you'd like to see all the columns present in the table. If, instead, you'd prefer to see one or two specific columns only in the result set, you can specify the column name(s) in the SELECT statement, like this:

    mysql> SELECT lname FROM members; +---------+ | lname | +---------+ | Doe | | Doe | | Klingon | | Claus | +---------+ 4 rows in set (0.00 sec)
    You can eliminate duplicate entries using the DISTINCT keyword - the following query will not display members with the last name "Doe" more than once.

    mysql> SELECT DISTINCT lname FROM members; +---------+ | lname | +---------+ | Doe | | Klingon | | Claus | +---------+ 3 rows in set (0.05 sec)


    This article copyright Melonfire 2001. All rights reserved. {mospagebreak title=Where, Oh Where, Art Thou?} Of course, the whole idea of structuring data into rows and columns is to make it easier to get a focused result set. And a great part of that focus comes from the WHERE clause (maybe you remember this from the UPDATE and DELETE statements you learnt last time) to the SELECT statement, which allows you to define specific criteria for the result set. Records that do not meet the specified criteria will not appear in the result set.

    For example, let's suppose that you wanted to see a list of all members with the last name "Doe".

    mysql> SELECT * FROM members WHERE lname = "Doe"; +-----------+-------+-------+---------+--------------------+ | member_id | fname | lname | tel | email | +-----------+-------+-------+---------+--------------------+ | 1 | John | Doe | 1234567 | jdoe@somewhere.com | | 2 | Jane | Doe | 8373728 | jane@site.com | +-----------+-------+-------+---------+--------------------+ 2 rows in set (0.00 sec)
    Or let's suppose that you wanted Santa Claus' email address:

    mysql> SELECT email FROM members WHERE fname = "Santa"; +----------------------------+ | email | +----------------------------+ | santa@the-north-pole.com | +----------------------------+ 1 row in set (0.06 sec)
    Or even that you wanted to see a list of all movies by George Lucas.

    mysql> SELECT title, director FROM videos WHERE director = "George Lucas"; +-------------------------------+--------------+ | title | director | +-------------------------------+--------------+ | Star Wars: The Phantom Menace | George Lucas | +-------------------------------+--------------+ 1 row in set (0.06 sec)
    Yes, I know the collection is incomplete. Maybe I should write to Santa for the other three.

    This article copyright Melonfire 2001. All rights reserved.

    More MySQL Articles
    More By icarus, (c) Melonfire


     

       

    MYSQL ARTICLES

    - 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...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway