MySQL
  Home arrow MySQL arrow Page 3 - 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) - Teacher's Pet


    (Page 3 of 8 )

    You can use relational and Boolean operators to modify your SQL query further - this comes in very handy if your table contains a large amount of numeric data, as illustrated below:

    # grades table +-------+------+---------+------------+ | name | math | physics | literature | +-------+------+---------+------------+ | john | 68 | 37 | 45 | | jim | 96 | 89 | 92 | | bill | 65 | 12 | 57 | | harry | 69 | 25 | 82 | +-------+------+---------+------------+
    The six relational operators available to you in SQL are as follows:

    OperatorWhat It Means
    =is equal to
    !=is not equal to
    >is greater than
    <is less than
    >=is greater than/equal to
    <=is less than/equal to


    You can also use the Boolean operators AND, OR and NOT to create more complex queries.
    Now, looking at the table above, if you wanted a list of all students who scored over 90 in their math paper, you could formulate a query which looked like this:

    mysql> SELECT * FROM grades WHERE math > 90; +------+------+---------+------------+ | name | math | physics | literature | +------+------+---------+------------+ | jim | 96 | 89 | 92 | +------+------+---------+------------+ 1 row in set (0.00 sec)
    Suppose you wanted to identify the smartest kid in class (you know this guy - he always sits in the front row, answers every question perfectly, and usually has wires on his teeth) so that you could beat him up during break.

    mysql> SELECT name FROM grades WHERE math > 85 AND physics > 85 AND literature > 85; +------+ | name | +------+ | jim | +------+ 1 row in set (0.00 sec)
    What if you needed to identify the ones who flunked at least one paper?

    mysql> SELECT * FROM grades WHERE math <= 25 OR physics <= 25 OR literature <= 25; +-------+------+---------+------------+ | name | math | physics | literature | +-------+------+---------+------------+ | bill | 65 | 12 | 57 | | harry | 69 | 25 | 82 | +-------+------+---------+------------+ 2 rows in set (0.00 sec)
    And finally, you can also perform basic mathematical operations within your query - the next example demonstrates how the three grades can be added together to create a total grade.

    mysql> SELECT name, math+physics+literature FROM grades; +-------+-------------------------+ | name | math+physics+literature | +-------+-------------------------+ | john | 150 | | jim | 277 | | bill | 134 | | harry | 176 | +-------+-------------------------+ 4 rows in set (0.05 sec)
    Obviously, such an operation should only be attempted on fields of the same type.

    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 6 hosted by Hostway