docs.roxen.comView this page in a printer friendly mode
DocsRoxen2.1TutorialsDatabase Tutorial
Copyright  2001, Roxen Internet Software
Suggestions, comments & compliments
manuals@roxen.com
 DEMO  DOCS  PIKE
 COMMUNITY  DOWNLOAD
www.roxen.com



Installing MySQL
Privileges
Building a Sample Database
The query() function
The big_query() function
Quoting
SQL Syntax
Conditions
Sorting
Limiting
Functions
Features Missing from MySQL
Insertion Syntax
The tablify Container
The Business Graphics Module
The emit and sqlquery Tags
Database Creation
Creating Tables
Indices
Dropping

Functions

Whenever a column or a constant can be used in a query definition, a function can be used instead. Functions perform operations on the data, the usual quoting rules applying to their arguments.

The available function and their names names vary wildly from server to server, as does their syntax. We'll introduce here the most important MySQL functions. For further information, consult your server's documentation.

Arithmetic and math functions
+, -(unary or binary), / (with infix notation), *

ABS(X)

SIGN(X)

MOD(X Y)

modulo, like 'X % Y' in C

FLOOR(X)

CEILING(X)

ROUND(X)

rounding operators

LEAST(X, Y,...)

returns the smallest of its arguments

GREATEST(X, Y,...)

returns the greatest of its arguments

Comparison and logic functions
=

equality

!= or <>

dis-equality

>, >=, <, <=

IS [NOT] NULL

true if the compared value is (not) NULL

expr IN (value, ...)

true if the expression expr appears in the list

NOT or !

OR or ||

AND or &&

logic operators

String comparison and operations
value LIKE pattern

see the Conditions page

value REGEXP pattern

performs a regular-expression match

CONCAT(str1, str2,...)

concatenates the arguments

LENGTH(str)

returns the length of its argument

LEFT(str,len)

return the leftmost len characters

RIGHT(str,len)

return the rightmost len characters

SUBSTRING(string,start_at,length)

returns length characters starting from position start_at

TRIM([LEADING|TRAILING|BOTH] FROM string)

trims leading, trailing or both spaces from string

LOWER(string)

returns the string in lower case

UPPER(string)

returns the string in upper case

PASSWORD(string)

returns a Mysql password that checks against string

ENCRYPT(string[,salt])

same as the Unix crypt(3) function. If supplied, 'salt' should be 2 characters long. Otherwise it uses a random salt.

Control flow operators
IFNULL(expr1,expr2)

if expr1 is not null, returns it, otherwise it returns expr2

IF(expr1,expr2,expr3)

if expr1 is true, returns expr2, else expr3

Date-related functions
DAYOFWEEK(date)

returns the weekday index for date (Sunday=1...Saturday=7)

DAYOFMONTH(date)

returns the day of the month for date (1..31)

DAYOFYEAR(date)

returns the day of the year for date (1..366)

MONTH(date)

returns the month for date (1..12)

YEAR(date)

returns the year from date (1000..9999)

HOUR(time), MINUTE(time), SECOND(time)

time extraction functions

CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP

'magic' variables, that are treated like functions. They contain the current date, time, and timestamp respectively.

Miscellaneous functions
LAST_INSERT_ID()

returns the last value automatically generated by an 'AUTO_INCREMENT'-type column

Special functions

These functions are somewhat 'special', in that they have different semantics when used in conjunction with the 'GROUP BY' clause (which is not covered in this manual).

COUNT([DISTINCT] expr)

if 'expr' is a column name, it returns the number of non-null rows returned for that column. If it's an asterisk '*', it gives the number of returned rows. If the DISTINCT keyword is specified, duplicate values are not counted.

AVG(expr)

Returns the average of the columns matched.

MIN(expr)

Returns the least of the columns matched.

MAX(expr)

Returns the greatest of the columns matched.

SUM(expr)

Returns the sum of the columns matched.

Show the current day of the week:

$ mysql -u user -p password sample
[MySQL Monitor]
> select dayofweek(now()) as day;

Count the number of rows in a table:

> select count(*) from ids;

Count the number of countries whose name begins by 'i':

> select count(*) as number from ids where ids.name like 'I%'