Chapter 8. Using MySQL Built-In Functions with SELECT


MySQL has a wide variety of built-in operators and functions that can be useful for writing queries. Most of these are for use in the SELECT and WHERE clauses. There are also some special grouping functions for use in the GROUP BY clause. We have already used the basic comparison operators and the count() and max() functions. A vast number of functions are available. In this chapter, we take a tour of the most useful ones. This book is not trying to be a function reference by any means ”we are just trying to give you a feel for the types of functionality available.

We will cover the following:

  • Operators

  • Control flow functions

  • String functions

  • Numeric functions

  • Date and time functions

  • Cast functions

  • Other functions

  • Functions for use with GROUP BY clauses

One important point to note is that, in MySQL, any expression containing NULL will evaluate to NULL , with a few exceptions we will note as we go along. We will discuss this further in the section on comparison operators.

In this chapter, we will make some use of the SELECT statement without any tables. We can use SELECT as a basic calculator. For example, if we type

 
 select 2+2; 

we will get the result

 
 +-----+  2+2  +-----+    4  +-----+ 1 row in set (0.42 sec) 

We can execute any expression without tables and have access to a full range of math and other operators and functions. Although the capability to execute 2+2 is trivial, the capability to do math at the SELECT level is not always so. For example, this lets you perform financial analysis of values in tables and display the results in a report.

In all MySQL expressions, you can use parentheses to control the order in which subexpressions are evaluated, as you would in any programming language.

We will begin by looking at the operators.



MySQL Tutorial
MySQL Tutorial
ISBN: 0672325845
EAN: 2147483647
Year: 2003
Pages: 261

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net