Appendix D. MySQL Functions
Having functions built into MySQL substantially reduces the speed of performing complex queries, as well as their complexity. If you wish to learn more about the available functions, you can visit the following URLs:
But, for easy reference, here are some of the most commonly used MySQL functions.
String Functions
CONCAT(
str1
,
str2
, ...)
Returns the result of concatenating
str1
,str2
, and any other parameters (orNULL
if any argument isNULL
). If any of the arguments are binary, then the result is a binary string; otherwise, the result is a non-binary string. The code returns the string"MySQL"
:SELECT CONCAT('My', 'S', 'QL');
CONCAT_WS(
separator
,
str1
,
str2
, ...)
This works in the same way as
CONCAT
except it inserts a separator between the items being concatenated. If the separator isNULL
the result will beNULL
, butNULL
values can be used as other arguments, which will then be skipped. This code returns the string"Truman,Harry,S"
:SELECT CONCAT_WS(',', 'Truman', 'Harry', 'S');
LEFT(
str
,
len
)
Returns the leftmost
len
characters from the stringstr
(orNULL
if any argument isNULL
). The following code returns the string"Chris"
:SELECT LEFT('Christopher Columbus', '5');
RIGHT(
str
,
len
)
Returns the rightmost
len
characters from the stringstr
(orNULL
if any argument isNULL
). This code returns the string"Columbus"
:SELECT RIGHT('Christopher Columbus', '8');
MID(
str
,
pos
,
len
)
Returns up to
len
characters from the stringstr
starting ...
Get Learning PHP, MySQL, JavaScript, CSS & HTML5, 3rd Edition now with O’Reilly online learning.
O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.