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  
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) - 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:

    Operator What 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

    - 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek