Mysql Functions
Mysql Functions
FUNCTIONS IN MYSQL
A function can be defined as a set of predefined commands when called performs certain computation &
return a value.
Single row functions operate on a single value and return a single value.
When applied to a table, they return a single result for every row of the queried table.
String functions
Numeric functions
Date & time functions
AGGREGATE FUNCTIONS
Function Description
MAX() Returns the maximum/highest value among the values in the given
column/expression.
MIN() Returns the minimum/lowest value among the values in the given column/expression.
SUM() Returns the sum of the values under the specified column/expression.
AVG() Returns the average of the values under the specified column/expression.
COUNT(*) Returns the total number of records in a table.
COUNT(column_name) Returns the total number of values in column.
MATH FUNCTIONS
NOW()
-------------------
2014-08-08 10:04:25
MONTH() returns the MONTH for the date within a range of 1 to 12 ( January to December)
SELECT MONTH('2009-05-18'); Output :
MONTH('2009-05-18')
------------------- -----
5
MONTHNAME() returns the full name of the month for a given date.
SELECT MONTHNAME('2009-05-18'); Output :
MONTHNAME('2009-05-18')
--------------------------------
May
DAY() returns the day of the month for a specified date.
SELECT DAY('2008-05-15'); Output :
DAY('2008-05-15')
-------------------
15
DAYNAME() returns the name of the week day of a date,.
SELECT DAYNAME('2008-05-15'); Output :
DAYNAME('2008-05-15')
---------------------------
Thursday
YEAR() returns the year for a given date.
SELECT YEAR('2009-05-19'); Output :
YEAR('2009-05-19')
---------------------
2009
Examples:
1) mysql>select round(2.25);
+---------------+
| round(2.25) |
+---------------+
|2|
+---------------+
1 row in set (0.01 sec)
2) mysql>select round(2.25, 1);
+------------------+
| round(2.25, 1) |
+------------------+
| 2.3 |
+------------------+
1 row in set (0.00 sec)
3) mysql>select round(2.25, 2);
+-------------------+
| round(2.25, 2) |
+------------------+
| 2.25 |
+------------------+
1 row in set (0.00 sec)
4) mysql>select round(2.26, 0);
+------------------+
| round(2.26, 0) |
+------------------+
|2|
+------------------+
1 row in set (0.00 sec)
5) mysql>select round(135.43, 0);
+----------------------+
| round(135.43, 0) |
+---------------------+
| 135 |
+---------------------+
1 row in set (0.00 sec)
+----------------------+
| round(135.53, 0) |
+----------------------+
| 136 |
+----------------------+
1 row in set (0.00 sec)
7) mysql>select round(135.55, 1);
+----------------------+
| round(135.55, 1) |
+----------------------+
| 135.6 |
+----------------------+
1 row in set (0.00 sec)
8) mysql>select round(135.55, -1);
+-----------------------+
| round(135.55, -1) |
+-----------------------+
| 140 |
+-----------------------+
1 row in set (0.00 sec)
9) mysql>select round(134.45, -1);
+------------------------+
| round(134.45, -1) |
+-----------------------+
| 130 |
+-----------------------+
1 row in set (0.00 sec)
10) mysql>select round(134.45, -2);
+------------------------+
| round(134.45, -2) |
+-----------------------+
| 100 |
+-----------------------+
1 row in set (0.00 sec)
+------------------------+
| round(154.45, -2) |
+-----------------------+
| 200 |
+-----------------------+
1 row in set (0.00 sec)
12) mysql>select round(1454.45, -2);
+------------------------+
| round(1454.45, -2) |
+------------------------+
| 1500 |
+------------------------+
1 row in set (0.00 sec)
13) mysql>select round(1444.45, -2);
+-------------------------+
| round(1444.45, -2) |
+------------------------+
| 1400 |
+------------------------+
1 row in set (0.00 sec)
14) mysql>select round(1444.45, -3);
+-------------------------+
| round(1444.45, -3) |
+-------------------------+
| 1000 |
+------------------------+
1 row in set (0.00 sec)
15) mysql>select round(1544.45, -3);
+-------------------------+
| round(1544.45, -3) |
+-------------------------+
| 2000 |
+-------------------------+
1 row in set (0.00 sec)