Functions for use in SELECT and WHERE
clause
Normal arithmetic operations
The usual arithmetic operators are
available. Note that in the case of-,
+
and *,
the result is calculated with BIGINT
(64-bit) precision if both arguments are integers!
Bit functions
MySQL uses
BIGINT
(64-bit) arithmetic for bit operations, so these operators have a maximum range
of 64 bits.
|
|
Bitwise OR |
&
|
Bitwise AND |
<<
|
Shifts a longlong (BIGINT)
number to the left. |
>>
|
Shifts a longlong (BIGINT)
number to the right. |
~
|
Invert all bits. |
BIT_COUNT(N)
|
Returns the number of bits that are
set in the argument N. |
Logical Functions
All logical functions return
1 (TRUE) or
0
(FALSE).
!
NOT
|
Logical NOT. Returns
1 if the
argument is 0,
otherwise returns0.
Exception: NOT NULL
returns NULL. |
||
OR
|
Logical OR. Returns
1 if
either argument is not 0
and not NULL. |
&&
AND
|
Logical AND. Returns
0 if
either argument is 0
or NULL,
otherwise returns 1. |
Comparsion Functions
Comparison operations result in a value of
1
(TRUE), 0
(FALSE) or NULL.
These functions work for both numbers and strings. Strings are automatically
converted to numbers and numbers to strings as needed (as in Perl).
MySQL performs comparisons using the
following rules:
- If one or both arguments are
NULL,
the result of the comparison is NULL,
except for the <=> operator. If both arguments in a comparison operation are
strings, they are compared as strings.
- If both arguments are integers, they
are compared as integers. Hexadecimal values are treated as binary strings if
not compared to a number.
- If one of the arguments is a
TIMESTAMP
or DATETIME
column and the other argument is a constant, the constant is converted to a
timestamp before the comparison is performed. This is done to be more
ODBC-friendly.
- In all other cases, the arguments are
compared as floating-point (real) numbers.
By default, string comparisons are done in
case-independent fashion using the current character set (ISO-8859-1 Latin1 by
default, which also works excellently for English).
=
|
Equal |
<>
!= |
Not equal |
<=
|
Less than or equal to |
<
|
Less than |
>=
|
Greater than or equal to |
>
|
Greater than |
<=>
|
Null safe equal |
IS NULL
IS NOT NULL |
Test whether or not a value is or is
not NULL |
expr BETWEEN min AND max
|
If
expr is
greater than or equal to min
and expr
is less than or equal to max,
BETWEEN
returns 1, otherwise it returns 0. This is equivalent to the expression (min
<= expr AND expr
<= max)
if all the arguments are of the same type. The first argument (expr)
determines how the comparison is performed as follows:
- If expr is a
TIMESTAMP,
DATE
or DATETIME
column, min
and max
are formatted to the same format if they are constants.
- If expr is a case-insensitive
string expression, a case-insensitive string comparison is done.
- If expr is a case-sensitive string
expression, a case-sensitive string comparison is done.
- If expr is an integer expression,
an integer comparison is done.
- Otherwise, a floating-point (real)
comparison is done.
|
expr IN (value,...)
|
Returns 1 if expr is any of the
values in the IN
list, else returns 0. If all values are constants, then all values are
evaluated according to the type of expr and sorted. The search for the item
is then done using a binary search. This means
IN is very
quick if the IN
value list consists entirely of constants. If expr is a case-sensitive
string expression, the string comparison is performed in case-sensitive
fashion. |
expr NOT IN (value,...)ISNULL(expr)
|
Same as
NOT (expr IN (value,...)).
|
ISNULL(expr)
|
If expr is
NULL,
ISNULL()
returns 1, otherwise it returns 0. |
COALESCE(list)
|
Returns first non-NULL
element in list. |
INTERVAL(N,N1,N2,N3,...)
|
Returns 0 if
N < N1, 1
if N < N2
and so on. All arguments are treated as integers. It is required that
N1 < N2 < N3 < ... < Nn
for this function to work correctly. This is because a binary search is used
(very fast). |
String Comparsion Functions
Normally, if any expression in a string
comparison is case sensitive, the comparison is performed in case-sensitive
fashion.
expr LIKE pat [ESCAPE
'escape-char'] |
Pattern matching using SQL simple
regular expression comparison. Returns 1 (TRUE) or 0 (FALSE). With LIKE you
can use the following two wildcard characters in the pattern:
- % Matches any number of characters,
even zero characters
- _ Matches exactly one character
To test for literal instances of a wildcard character, precede the character
with the escape character. If you don't specify the
ESCAPE
character, `' is assumed:
- % Matches one % character
- _ Matches one _ character
Use the ESCAPE
clause to specify a different escape character.
LIKE
is allowed on numeric expressions! (This is a MySQL
extension to the ANSI SQL LIKE.)
Note: Because MySQL uses the C escape syntax in strings (e.g., `n'), you
must double any `' that you use in your
LIKE
strings. For example, to search for `n', specify it as `\n'. To search for
`', specify it as `\\' (the backslashes are stripped once by the parser, and
another time when the pattern match is done, leaving a single backslash to
be matched).
|
expr NOT LIKE pat [ESCAPE
'escape-char'] |
Same as
NOT (expr LIKE pat [ESCAPE 'escape-char']). |
expr REGEXP pat
expr RLIKE pat |
Performs a pattern match of a string
expression expr against a pattern pat. The pattern can be an extended
regular expression. Returns 1 if expr matches pat, otherwise returns 0.
RLIKE
is a synonym for REGEXP,
provided for mSQL compatibility. Note: Because MySQL uses the C escape
syntax in strings (e.g., `n'), you must double any `' that you use in your
REGEXP strings. In MySQL 3.23.4 REGEXP is case insensitive for normal (not
binary) strings. REGEXP
and RLIKE
use the current character set (ISO-8859-1 Latin1 by default) when deciding
the type of a character. |
expr NOT REGEXP pat
expr NOT RLIKE pat |
Same as
NOT (expr REGEXP pat). |
STRCMP(expr1,expr2)
|
STRCMP()
returns 0 if the strings are the same, -1 if the first argument is smaller
than the second according to the current sort order, and 1 otherwise. |
Cast operators
BINARY
|
The
BINARY
operator casts the string following it to a binary string. This is an easy
way to force a column comparison to be case sensitive even if the column
isn't defined as BINARY
or BLOB.
BINARY
was introduced in MySQL 3.23.0. |
Control flow functions
IFNULL(expr1,expr2)
|
If expr1 is not
NULL,
IFNULL()
returns expr1,
else it returns expr2.
IFNULL()
returns a numeric or string value, depending on the context in which it is
used. |
NULLIF(expr1,expr2)
|
If
expr1 = expr2
is true, return NULL
else return expr1. This is the same as
CASE WHEN x = y THEN NULL ELSE x END |
IF(expr1,expr2,expr3)
|
If expr1 is
TRUE (expr1
<> 0 and expr1
<> NULL)
then IF()
returns expr2, else it returns expr3.
IF()
returns a numeric or string value, depending on the context in which it is
used. |
CASE value WHEN [compare-value]
THEN result [WHEN [compare-value] THEN result ...] [ELSE result] END
|
Returns the result where
value=compare-value. |
CASE WHEN [condition] THEN
result [WHEN [condition] THEN result ...] [ELSE result] END
|
Returns the result for the first
condition which is true. If there was no matching result value, then the
result after ELSE
is returned. If there is no ELSE
part then NULL
is returned. |
Mathematical functions
All mathematical functions return
NULL in
case of an error.
ABS(X)
|
Returns the absolute value of X. |
SIGN(X)
|
Returns the sign of the argument as
-1, 0 or 1, depending on whether X is negative, zero, or positive. |
MOD(N,M)
% |
Modulo (like the % operator in C).
Returns the remainder of N
divided by M. |
FLOOR(X)
|
Returns the largest integer value not
greater than X. |
CEILING(X)
|
Returns the smallest integer value
not less than X. |
ROUND(X)
|
Returns the argument X, rounded to
the nearest integer. |
ROUND(X,D)
|
Returns the argument
X, rounded
to a number with D
decimals. If D
is 0, the result will have no decimal point or fractional part. |
EXP(X)
|
Returns the value of
e (the
base of natural logarithms) raised to the power of
X. |
LOG(X)
|
Returns the natural logarithm of
X. |
LOG10(X)
|
Returns the base-10 logarithm of
X. |
POW(X,Y)
POWER(X,Y) |
Returns the value of X raised to the
power of Y. |
SQRT(X)
|
Returns the non-negative square root
of X. |
PI()
|
Returns the value of PI. |
COS(X)
|
Returns the cosine of
X, where
X
is given in radians. |
SIN(X)
|
Returns the sine of
X, where
X
is given in radians. |
TAN(X)
|
Returns the tangent of
X, where
X
is given in radians. |
ACOS(X)
|
Returns the arc cosine of
X,
that is, the value whose cosine is X.
Returns NULL
if X
is not in the range -1 to 1. |
ASIN(X)
|
Returns the arc sine of
X, that
is, the value whose sine is X.
Returns NULL
if X
is not in the range -1 to 1. |
ATAN(X)
|
Returns the arc tangent of
X,
that is, the value whose tangent is X. |
ATAN2(X,Y)
|
Returns the arc tangent of the two
variables X and Y. It is similar to calculating the arc tangent of Y / X,
except that the signs of both arguments are used to determine the quadrant
of the result. |
COT(X)
|
Returns the cotangent of X. |
RAND()
RAND(N) |
Returns a random floating-point value
in the range 0 to 1.0. If an integer argument
N is
specified, it is used as the seed value.
You can't use a column with RAND()
values in an ORDER BY
clause, because ORDER BY
would evaluate the column multiple times. In MySQL 3.23,
you can however do: SELECT * FROM
table_name ORDER BY RAND() This is
useful to get a random sample of a set
SELECT * FROM table1,table2 WHERE a=b AND c<d
ORDER BY RAND() LIMIT 1000. Note
that a RAND()
in a WHERE
clause will be re-evaluated every time the
WHERE is
executed. |
LEAST(X,Y,...)
|
With two or more arguments, returns
the smallest (minimum-valued) argument. The arguments are compared using the
following rules:
- If the return value is used in an
INTEGER
context, or all arguments are integer-valued, they are compared as
integers.
- If the return value is used in a
REAL
context, or all arguments are real-valued, they are compared as reals.
- If any argument is a case-sensitive
string, the arguments are compared as case-sensitive strings.
- In other cases, the arguments are
compared as case-insensitive strings.
|
GREATEST(X,Y,...)
|
Returns the largest (maximum-valued)
argument. The arguments are compared using the same rules as for
LEAST. |
DEGREES(X)
|
Returns the argument
X,
converted from radians to degrees. |
RADIANS(X)
|
Returns the argument
X,
converted from degrees to radians. |
TRUNCATE(X,D)
|
Returns the number
X,
truncated to D
decimals. If D
is 0, the result will have no decimal point or fractional part. |
String functions
String-valued functions return
NULL if
the length of the result would be greater than the max_allowed_packet server
parameter.
For functions that operate on string
positions, the first position is numbered 1.
MySQL automatically
converts numbers to strings as necessary, and vice versa.
If you want to convert a number to a
string explicitly, pass it as the argument to CONCAT().
If a string function is given a binary
string as an argument, the resulting string is also a binary string. A number
converted to a string is treated as a binary string. This only affects
comparisons.
ASCII(str)
|
Returns the ASCII code value of the
leftmost character of the string str.
Returns 0 if str is the empty string.
Returns NULL if str is NULL. |
ORD(str)
|
If the leftmost character of the
string str is a multi-byte character, returns the code of multi-byte
character by returning the ASCII code value of the character in the format
of: ((first byte ASCII
code)*256+(second byte ASCII code))[*256+third byte ASCII code...].
If the leftmost character is not a multi-byte character, returns the same
value as the like ASCII() function does. |
CONV(N,from_base,to_base)
|
Converts numbers between different
number bases. Returns a string representation of the number
N,
converted from base from_base
to base to_base.
Returns NULL
if any argument is NULL.
The argument N
is interpreted as an integer, but may be specified as an integer or a
string. The minimum base is 2 and the maximum base is 36. If
to_base is
a negative number, N
is regarded as a signed number. Otherwise,
N is
treated as unsigned. CONV
works with 64-bit precision. |
BIN(N)
|
Returns a string representation of
the binary value of N,
where N
is a longlong (BIGINT)
number. This is equivalent to
CONV(N,10,2). Returns
NULL if
N
is NULL. |
OCT(N)
|
Returns a string representation of
the octal value of N,
where N
is a longlong number. This is equivalent to
CONV(N,10,8).
Returns NULL
if N is NULL. |
HEX(N)
|
Returns a string representation of
the hexadecimal value of N,
where N
is a longlong (BIGINT)
number. This is equivalent to
CONV(N,10,16). Returns
NULL if
N
is NULL. |
CHAR(N,...)
|
CHAR()
interprets the arguments as integers and returns a string consisting of the
characters given by the ASCII code values of those integers.
NULL
values are skipped. |
CONCAT(str1,str2,...)
|
Returns the string that results from
concatenating the arguments. Returns
NULL if any argument is
NULL.
May have more than 2 arguments. A numeric argument is converted to the
equivalent string form. |
CONCAT_WS(separator, str1,
str2,...) |
CONCAT_WS()
stands for CONCAT With Separator and is a special form of
CONCAT().
The first argument is the separator for the rest of the arguments. The
separator can be a string as well as the rest of the arguments. If the
separator is NULL,
the result will be NULL.
The function will skip any NULLs
and empty strings, after the separator argument. The separator will be added
between the strings to be concatenated. |
LENGTH(str)
|
Returns the length of the string
str. |
OCTET_LENGTH(str)
|
As same as
LENGTH(str). |
CHAR_LENGTH(str)
|
As same as
LENGTH(str)
except multi-byte characters are only counted once. |
CHARACTER_LENGTH(str)
|
As same as
LENGTH(str). |
LOCATE(substr,str)
|
Returns the position of the first
occurrence of substring substr
in string str.
Returns 0 if substr
is not in str. |
POSITION(substr IN str)
|
As same as
LOCATE(substr,str). |
LOCATE(substr,str,pos)
|
Returns the position of the first
occurrence of substring substr
in string str,
starting at position pos.
Returns 0 if substr
is not in str.
This function is multi-byte safe. |
INSTR(str,substr)
|
Returns the position of the first
occurrence of substring substr
in string str.
This is the same as the two-argument form of
LOCATE(),
except that the arguments are swapped. This function is multi-byte safe.
|
LPAD(str,len,padstr)
|
Returns the string
str,
left-padded with the string padstr
until str
is len
characters long. |
RPAD(str,len,padstr)
|
Returns the string
str,
right-padded with the string padstr
until str
is len
characters long. |
LEFT(str,len)
|
Returns the leftmost
len
characters from the string str.
This function is multi-byte safe. |
RIGHT(str,len)
|
Returns the rightmost
len
characters from the string str.
This function is multi-byte safe. |
SUBSTRING(str,pos,len)
|
Returns a substring
len
characters long from string str,
starting at position pos.
This function is multi-byte safe. |
MID(str,pos,len)
|
Same as
SUBSTRING(str,pos,len). |
SUBSTRING(str FROM pos FOR len)
|
Same as
SUBSTRING(str,pos,len)
except this variant is ANSI SQL92 syntax. |
SUBSTRING(str,pos)
|
Returns a substring from string
str
starting at position pos. |
SUBSTRING(str FROM pos)
|
Same as
SUBSTRING(str,pos).
This function is multi-byte safe. |
SUBSTRING_INDEX(str,delim,count)
|
Returns the substring from string
str
before count
occurrences of the delimiter delim.
If count
is positive, everything to the left of the final delimiter (counting from
the left) is returned. If count
is negative, everything to the right of the final delimiter (counting from
the right) is returned. This function is multi-byte safe. |
LTRIM(str)
|
Returns the string
str with
leading space characters removed. |
RTRIM(str)
|
Returns the string
str with
trailing space characters removed. This function is multi-byte safe. |
TRIM([[BOTH | LEADING |
TRAILING]
[remstr] FROM] str) |
Returns the string
str with
all remstr
prefixes and/or suffixes removed. If none of the specifiers
BOTH,
LEADING
or TRAILING
are given, BOTH
is assumed. If remstr
is not specified, spaces are removed. This function is multi-byte safe. |
SOUNDEX(str)
|
Returns a soundex string from
str.
Two strings that sound ``about the same'' should have identical
soundex
strings. A ``standard'' soundex string is 4 characters long, but the
SOUNDEX()
function returns an arbitrarily long string. You can use
SUBSTRING()
on the result to get a ``standard'' soundex string. All non-alphanumeric
characters are ignored in the given string. All international alpha
characters outside the A-Z range are treated as vowels. |
SPACE(N)
|
Returns a string consisting of
N
space characters. |
REPLACE(str,from_str,to_str)
|
Returns the string
str with
all all occurrences of the string
from_str replaced by the string
to_str.
This function is multi-byte safe. |
REPEAT(str,count)
|
Returns a string consisting of the
string str
repeated count
times. If count
<= 0, returns an empty string. Returns
NULL if
str or count are NULL. |
REVERSE(str)
|
Returns the string
str with
the order of the characters reversed. This function is multi-byte safe. |
INSERT(str,pos,len,newstr)
|
Returns the string
str, with
the substring beginning at position
pos and
len
characters long replaced by the string
newstr.
This function is multi-byte safe. |
ELT(N,str1,str2,str3,...)
|
Returns
str1 if
N
= 1, str2
if N
= 2, and so on. Returns NULL
if N
is less than 1 or greater than the number of arguments.
ELT() is
the complement of FIELD(). |
FIELD(str,str1,str2,str3,...)
|
Returns the index of str in the
str1,
str2,
str3,
... list. Returns 0 if str
is not found. FIELD()
is the complement of ELT(). |
FIND_IN_SET(str,strlist)
|
Returns a value 1 to
N if the
string str
is in the list strlist
consisting of N
substrings. A string list is a string composed of substrings separated by
`,' characters. If the first argument is a constant string and the second is
a column of type SET,
the FIND_IN_SET()
function is optimized to use bit arithmetic! Returns 0 if
str is not
in strlist
or if strlist
is the empty string. Returns NULL
if either argument is NULL.
This function will not work properly if the first argument contains a `,'. |
MAKE_SET(bits,str1,str2,...)
|
Returns a set (a string containing
substrings separated by `,' characters) consisting of the strings that have
the corresponding bit in bits
set. str1
corresponds to bit 0, str2
to bit 1, etc. NULL
strings in str1,
str2,
... are not appended to the result. |
EXPORT_SET(bits,on,off,
[separator,[number_of_bits]])
|
Returns a string where for every bit
set in 'bit', you get a 'on' string and for every reset bit you get an 'off'
string. Each string is separated with 'separator' (default ',') and only 'number_of_bits'
(default 64) of 'bits' is used. |
LCASE(str)
|
Returns the string
str with
all characters changed to lowercase according to the current character set
mapping (the default is ISO-8859-1 Latin1). This function is multi-byte
safe. |
LOWER(str)
|
Same as LCASE(str). |
UCASE(str)
|
Returns the string
str with
all characters changed to uppercase according to the current character set
mapping (the default is ISO-8859-1 Latin1). |
UPPER(str)
|
Same as UCASE(str). |
LOAD_FILE(file_name)
|
Reads the file and returns the file
contents as a string. The file must be on the server, you must specify the
full pathname to the file, and you must have the file
privilege. The file must be readable by all and be smaller than
max_allowed_packet.
If the file doesn't exist or can't be read due to one of the above reasons,
the function returns NULL.
If you are not using MySQL 3.23, you have to do the reading
of the file inside your application and create an
INSERT
statement to update the database with the file information. |
Date and time functions
DAYOFWEEK(date)
|
Returns the weekday index for
date
(1 = Sunday, 2 = Monday, ... 7 = Saturday). These index values correspond to
the ODBC standard. |
WEEKDAY(date)
|
Returns the weekday index for
date
(0 = Monday, 1 = Tuesday, ... 6 = Sunday). |
DAYOFMONTH(date)
|
Returns the day of the month for
date,
in the range 1 to 31. |
DAYOFYEAR(date)
|
Returns the day of the year for
date,
in the range 1 to 366. |
MONTH(date)
|
Returns the month for
date, in
the range 1 to 12. |
DAYNAME(date)
|
Returns the name of the weekday for
date. |
MONTHNAME(date)
|
Returns the name of the month for
date. |
QUARTER(date)
|
Returns the quarter of the year for
date,
in the range 1 to 4. |
WEEK(date)
|
Returns the week for
date, in
the range 0 to 53 (yes, there may be the beginnings of a week 53), for
locations where Sunday is the first day of the week. |
WEEK(date,first)
|
Allows you to specify whether the
week starts on Sunday or Monday. The week starts on Sunday if the second
argument is 0, on Monday if the second argument is 1. |
YEAR(date)
|
Returns the year for
date, in
the range 1000 to 9999. |
YEARWEEK(date)
|
Returns year and week for a
date. |
YEARWEEK(date,first)
|
Works exactly like the second
argument to WEEK().
Note that the year may be different from the year in the
date
argument for the first and the last week of the year! |
HOUR(time)
|
Returns the hour for
time, in
the range 0 to 23. |
MINUTE(time)
|
Returns the minute for
time, in
the range 0 to 59. |
SECOND(time)
|
Returns the second for
time, in
the range 0 to 59. |
PERIOD_ADD(P,N)
|
Adds
N months
to period P
(in the format YYMM or YYYYMM). Returns a value in the format YYYYMM. Note
that the period argument P
is not a date value. |
PERIOD_DIFF(P1,P2)
|
Returns the number of months between
periods P1
and P2.
P1
and P2
should be in the format YYMM or YYYYMM. Note that the period arguments
P1
and P2
are not date values. |
DATE_ADD(date,INTERVAL expr
type)
DATE_SUB(date,INTERVAL expr type)
ADDDATE(date,INTERVAL expr type)
SUBDATE(date,INTERVAL expr type)
|
These functions perform date
arithmetic. They are new for MySQL 3.22.
ADDDATE()
and SUBDATE()
are synonyms for DATE_ADD()
and DATE_SUB().
In MySQL 3.23, you can use + and - instead of
DATE_ADD()
and DATE_SUB().
date
is a DATETIME
or DATE
value specifying the starting date.
expr is an expression specifying the
interval value to be added or substracted from the starting date.
expr
is a string; it may start with a `-' for negative intervals. type is a
keyword indicating how the expression should be interpreted. The
EXTRACT(type FROM date)
function returns the 'type' interval from the date.
The following type
are valid: SECOND,
MINUTE,
HOUR,
DAY,
MONTH,
YEAR,
MINUTE_SECOND,
HOUR_MINUTE,
DAY_HOUR,
YEAR_MONTH,
HOUR_SECOND,
DAY_MINUTE,
and DAY_SECOND.
MySQL allows any punctuation delimiter in the
expr
format. If the date
argument is a DATE
value and your calculations involve only
YEAR,
MONTH
and DAY
parts (that is, no time parts), the result is a
DATE
value. Otherwise the result is a
DATETIME value.
If you specify an interval value that is too short (does not include all the
interval parts that would be expected from the type keyword), MySQL
assumes you have left out the leftmost parts of the interval value. Note
that if you add or subtract a date value against something that contains a
time part, the date value will be automatically converted to a datetime
value.
If you use really incorrect dates, the result is
NULL. If
you add MONTH,
YEAR_MONTH
or YEAR
and the resulting date has a day that is larger than the maximum day for the
new month, the day is adjusted to the maximum days in the new month.
|
TO_DAYS(date)
|
Given a date
date,
returns a daynumber (the number of days since year 0).
TO_DAYS()
is not intended for use with values that precede the advent of the Gregorian
calendar (1582), because it doesn't take into account the days that were
lost when the calender was changed. |
FROM_DAYS(N)
|
Given a daynumber
N, returns
a DATE
value. FROM_DAYS()
is not intended for use with values that precede the advent of the Gregorian
calendar (1582), because it doesn't take into account the days that were
lost when the calender was changed. |
DATE_FORMAT(date,format)
|
Formats the
date value
according to the format
string. |
TIME_FORMAT(time,format)
|
This is used like the
DATE_FORMAT()
function above, but the format string may contain only those format
specifiers that handle hours, minutes and seconds. Other specifiers produce
a NULL
value or 0. |
CURDATE()
|
Returns today's date as a value in 'YYYY-MM-DD'
or YYYYMMDD format, depending on whether the function is used in a string or
numeric context. |
CURRENT_DATE
|
Same as
CURDATE(). |
NOW()
|
Returns the current date and time as
a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS format, depending on
whether the function is used in a string or numeric context. |
SYSDATE()
|
Same as
NOW(). |
CURRENT_TIMESTAMP()
|
Same as
NOW(). |
UNIX_TIMESTAMP()
|
Returns a Unix timestamp (seconds
since '1970-01-01 00:00:00' GMT). |
UNIX_TIMESTAMP(date)
|
Returns the value of the argument as
seconds since '1970-01-01 00:00:00' GMT.
date may
be a DATE
string, a DATETIME
string, a TIMESTAMP,
or a number in the format YYMMDD or YYYYMMDD in local time. When
UNIX_TIMESTAMP
is used on a TIMESTAMP
column, the function will receive the value directly, with no implicit
``string-to-unix-timestamp'' conversion. If you give
UNIX_TIMESTAMP()
a wrong or out-of-range date, it will return 0. |
FROM_UNIXTIME(unix_timestamp,format)
|
Returns a string representation of
the Unix timestamp, formatted according to the format string.
format may
contain the same specifiers as those listed in the entry for the
DATE_FORMAT()
function. |
SEC_TO_TIME(seconds)
|
Returns the
seconds
argument, converted to hours, minutes and seconds, as a value in 'HH:MM:SS'
or HHMMSS format, depending on whether the function is used in a string or
numeric context. |
TIME_TO_SEC(time)
|
Returns the
time
argument, converted to seconds. |
Miscellaneous functions
DATABASE()
|
Returns the current database name. If
there is no current database,
DATABASE() returns the empty string. |
USER()
|
Returns the current MySQL
user name. |
SYSTEM_USER()
|
Same as
USER(). |
SESSION_USER()
|
Same as
USER(). |
PASSWORD(str)
|
Calculates a password string from the
plaintext password str.
This is the function that is used for encrypting MySQL passwords for storage
in the Password column of the user grant table. PASSWORD() encryption is
non-reversible. PASSWORD()
does not perform password encryption in the same way that Unix passwords are
encrypted. You should not assume that if your Unix password and your MySQL
password are the same, PASSWORD()
will result in the same encrypted value as is stored in the Unix password
file. |
ENCRYPT(str[,salt])
|
Encrypt
str using
the Unix crypt()
system call. The salt
argument should be a string with two characters. (As of MySQL
3.22.16, salt
may be longer than two characters.) If
crypt() is
not available on your system,
ENCRYPT() always returns
NULL.
ENCRYPT()
ignores all but the first 8 characters of
str, at
least on some systems. This will be determined by the behavior of the
underlying crypt()
system call. |
ENCODE(str,pass_str)
|
Encrypt
str using
pass_str
as the password. To decrypt the result, use
DECODE().
The results is a binary string of the same length as string. If you want to
save it in a column, use a BLOB
column type. |
DECODE(crypt_str,pass_str)
|
Decrypts the encrypted string
crypt_str
using pass_str
as the password. crypt_str
should be a string returned from
ENCODE(). |
|
MD5(string)
|
Calculates a MD5 checksum for the
string.
Value is returned as a 32 long hex number. |
LAST_INSERT_ID([expr])
|
Returns the last automatically
generated value that was inserted into an
AUTO_INCREMENT
column. The last ID that was generated is maintained in the server on a
per-connection basis. It will not be changed by another client. It will not
even be changed if you update another
AUTO_INCREMENT
column with a non-magic value (that is, a value that is not
NULL and
not 0). If expr
is given as an argument to
LAST_INSERT_ID() in an
UPDATE
clause, then the value of the argument is returned as a
LAST_INSERT_ID()
value. |
FORMAT(X,D)
|
Formats the number
X to a
format like '#,###,###.##', rounded to
D
decimals. If D
is 0, the result will have no decimal point or fractional part. |
VERSION()
|
Returns a string indicating the
MySQL server version. Note that if your version ends with
-log
this means that logging is enabled. |
CONNECTION_ID()
|
Returns the connection id (thread_id)
for the connection. Every connection has its own unique id. |
GET_LOCK(str,timeout)
|
Tries to obtain a lock with a name
given by the string str,
with a timeout of timeout
seconds. Returns 1 if the lock was obtained successfully, 0 if the attempt
timed out, or NULL
if an error occurred (such as running out of memory or the thread was killed
with mysqladmin kill).
A lock is released when you execute
RELEASE_LOCK(), execute a new
GET_LOCK()
or the thread terminates. This function can be used to implement application
locks or to simulate record locks. It blocks requests by other clients for
locks with the same name; clients that agree on a given lock string name can
use the string to perform cooperative advisory locking. |
RELEASE_LOCK(str)
|
Releases the lock named by the string
str
that was obtained with GET_LOCK().
Returns 1 if the lock was released, 0 if the lock wasn't locked by this
thread (in which case the lock is not released) and
NULL if
the named lock didn't exist. The lock will not exist if it was never
obtained by a call to GET_LOCK()
or if it already has been released. |
BENCHMARK(count,expr)
|
The
BENCHMARK()
function executes the expression expr
repeatedly count
times. It may be used to time how fast MySQL processes the
expression. The result value is always 0. The intended use is in the mysql
client, which reports query execution times. The time reported is elapsed
time on the client end, not CPU time on the server end. It may be advisable
to execute BENCHMARK()
several times, and interpret the result with regard to how heavily loaded
the server machine is. |
INET_NTOA(expr)
|
Returns the network address (4 or 8
byte) for the numeric expression. |
INET_ATON(expr)
|
Returns an integer that represents
the numeric value for a network address Addresses may be 4 or 8 byte
addresses. |
|