MySQL
  Home arrow MySQL arrow Page 4 - Optimizing Queries with Operators, Bra...
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

Optimizing Queries with Operators, Branching and Functions
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2006-03-30

    Table of Contents:
  • Optimizing Queries with Operators, Branching and Functions
  • Optimization 1: Separation of Logic and Formatting
  • MySQL Operators
  • Type Conversions with Logical and Arithmetic Operators
  • Operators for Working with Sets
  • Type Conversions in Comparisons

  • 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


    Optimizing Queries with Operators, Branching and Functions - Type Conversions with Logical and Arithmetic Operators


    (Page 4 of 6 )

    Here are a few rules to keep in mind when working with logical and arithmetic operators in MySQL:

    • Any nonzero integer used with a logical operator evaluates as TRUE, even if it has a negative value.
    • When used with logical operators, fractional values are rounded to the nearest integer. This means that any N where 0.5  > ABS(N)  = 0 evaluates as FALSE. (In other words, any number whose absolute value is less than 0.5 is rounded to 0 when used with a logical operator.)
    • When adding, subtracting, multiplying, or dividing a mix of integers and decimals, the result will always be a decimal; 3 * 2.33 returns 6.99, and the expression 2.1 + 1.145 - 3.245 yields 0.000.
    • Any string value used in a logical or arithmetic context always evaluates to 0 (FALSE). For example, 'a' AND 1 returns 0 (FALSE) and 'b' + 4 returns 4.
    • Any arithmetic expression containing NULL returns NULL . This includes the expressions +NULL and -NULL .
    • Division by zero produces a NULL result.
    • Operators (like all MySQL keywords) are case-insensitive. So it makes no practical difference whether you write A AND B , A and B , or A && B. However, as stated elsewhere, we generally use uppercase for MySQL keywords throughout this book for the sake of easy recognition and consistency.

    While this may all look quite familiar, be aware that what is true in your programming or scripting language of choice may not be so in MySQL. So, if you encounter what seems to be a bug in using arithmetic operators in a query, it may just be that MySQL behaves a bit differently than what you’re used to.

    Comparison Operators

    Like all programming languages, MySQL’s dialect of SQL has a more or less usual set of comparison operators, as well as a few unusual ones. The equals sign ( = ) serves as both the equality operator and the assignment operator, and an expression containing it evaluates to TRUE (1) if the operands are the same, and FALSE (0) if they’re not. If at least one of the operands is NULL , then the result is always NULL .

    As mentioned in our discussion of datatypes in Chapter 2, a special “null-safe” comparison operator, <=> , returns 1 (TRUE) if the arguments are equal values or if both values are NULL , and 0 (FALSE) otherwise. In other words, 1 <=> 1 returns TRUE (1), 1 <=> NULL returns FALSE (0), and NULL <=> NULL returns TRUE (1). (Remember that the NULL value is not equal to any other value, not even itself.)


    TIP
    IS [NOT] NULL for testing whether or not a value is NULL , as discussed in Chapter 3. IS NULL can also be written as ISNULL() ; for example, SELECT ISNULL(1); returns 0. You are likely to find that ISNULL() is more portable to and from other databases than IS NULL or the <=> operator. In addition, MySQL supports the IFNULL() and NULLIF() flow-control operators that can be used in connection with null values, as discussed in the “Branching: Making Choices in Queries” section later in this chapter.

    There are two ways of writing the inequality comparator. You may use either != or <>; they’re exactly the same so far as MySQL is concerned. The <> notation seems to be more common, and it’s what we prefer to use ourselves, but the choice is entirely up to you. (As always, we recommend you choose one or the other, and stick with it.) Note that NULL compared with any value—even itself—using <> or != yields the null value. However, when performing branching operations, you’re generally checking to see whether a condition is true or not, and since NULL isn’t true, a null result still causes you to follow the “false” branch.

    We’ll discuss IF() and the other branching operators later in this chapter.


    NOTE 
    Both forms of the inequality operator (<> and !=) are standard SQL and supported by most other relational databases.

    MySQL also has greater-than, less-than, greater-than-or-equal, and less-than-or-equal operators, which are written (in order) >, <, >=, and <=. These behave more or less just as you would expect. As with the equality and inequality operators, any comparison involving NULL and using one of these operators will always yield a NULL result.


    CAUTION  
    Some relational databases provide additional comparison operators meaning “not greater than” (!> or />) and “not less than” (!< or /< ). These are not part of the SQL standard and are not currently supported in MySQL.

    There’s also a shortcut for determining whether a given value lies within a certain range. The BETWEEN operator is used as shown here.

    BETWEEN can also be used with nonnumeric datatypes such as strings. Note that (as usual) string comparisons are case-insensitive unless you employ the BINARY modifier.

    Dates as well as strings can be compared.

    If the value immediately before the AND is not less than the value following it, then 0 will be always be returned.


    NOTE  
    There’s an additional operator for use in comparing strings.The LIKE operator allows you to do “fuzzy” matching with wildcards to find strings that are similar to one another. We’ll discuss LIKE in more detail in the “String Functions and Regular Expressions” section later in this chapter.

    More MySQL Articles
    More By Apress Publishing


       · This article is an excerpt from the book "Beginning MySQL Database Design and...
       · This article is really a helping hand for the developers using Mysql. It encourages...
       · Thank you! I'm glad you found it useful.
     

    Buy this book now. This article is excerpted from chapter four of Beginning MySQL Database Design and Optimization: From Novice to Professional, written by Jon Stephens and Chad Russell (Apress; ISBN: 1590593324). Check it out at your favorite bookstore today. Buy this book now.

       

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