Common functions in MySQL

A date function

1, NOW () returns the current date and time

mysql> SELECT NOW();
+---------------------+
| NOW()               |
+---------------------+
| 2019-10-19 10:55:28 |
+---------------------+
1 row in set (0.00 sec)

2, DATE_FORMAT (d, f) shows the date of the expression f d as required

MySQL >  the SELECT the DATE_FORMAT (the NOW (), ' % of the Y -% m Month - Day% d% H:% i points:% s s ' );
 + - ----------------------- + -------------------------------------- 
| the DATE_FORMAT (the NOW (), ' % the Y-years -% m month - day% d% H:% i points:% s s ' ) | 
+ - ------------------------- + ------------------------- 
| 2019 Nian - October - at 11:00 on the 19th: 03 minutes: 01 seconds                     | 
+ - --- + ----------------------------------------------- 
1 Row in  the SET ( 0.00 sec)

3, DATE_ADD (d, INTERVAL expr type) d is calculated by the date after the date of the start of a period of time

mysql> SELECT DATE_ADD(NOW(),INTERVAL 1 DAY); 
+--------------------------------+
| DATE_ADD(NOW(),INTERVAL 1 DAY) |
+--------------------------------+
| 2019-10-20 11:07:47            |
+--------------------------------+
1 row in set (0.00 sec)

Note: \ expr: positive (plus), negative (minus)
          \ Unit: support millisecond microsecond, seconds, second, hour hour, day day, week week, in year

Second, String Functions

1, CONCAT (s1, s2 ... sn) String s1, s2, more strings into a string

mysql> SELECT CONCAT('My','SQL');
+--------------------+
| CONCAT('My','SQL') |
+--------------------+
| MySQL              |
+--------------------+
1 row in set (0.00 sec)

2, FIELD (s, s1, s2 ...) Returns a string s position in the string list (s1, s2 ...) of

mysql> SELECT FIELD('C','A','B','C','D');
+----------------------------+
| FIELD('C','A','B','C','D') |
+----------------------------+
|                          3 |
+----------------------------+
1 row in set (0.00 sec)

3, LOWER (s) of the string s into all letters lower case letters

mysql> SELECT LOWER('ABCD');
+---------------+
| LOWER('ABCD') |
+---------------+
| abcd          |
+---------------+
1 row in set (0.00 sec)

4, REVERSE (s) will turn the order of the string s

mysql> SELECT REVERSE('ABCD');
+-----------------+
| REVERSE('ABCD') |
+-----------------+
| DCBA            |
+-----------------+
1 row in set (0.00 sec)

Third, the digital function

1, ABS (x) returns the absolute value of x

mysql> SELECT ABS(-10);
+----------+
| ABS(-10) |
+----------+
|       10 |
+----------+
1 row in set (0.00 sec)

2, ROUND (x) returns the nearest integer from x

mysql> SELECT ROUND(1.2345);
+---------------+
| ROUND(1.2345) |
+---------------+
|             1 |
+---------------+
1 row in set (0.00 sec)

3, RAND () returns a random number between 0 and 1

mysql> SELECT RAND();
+--------------------+
| RAND()             |
+--------------------+
| 0.5442781900468079 |
+--------------------+
1 row in set (0.00 sec)

4, POW (x, y) returns x to the power y

MySQL >  the SELECT the POW ( . 4 , 2 );   # 2. 4 th
 + - -------- + 
| the POW ( . 4 , 2 ) | 
+ - -------- + 
|        16  | 
+ - -------- + 
1 Row in  the SET ( 0.00 sec)

Fourth, aggregate functions

1, AVG (expression) Returns the average value of an expression, expression is a field

# Return Products table Price field average of 
MySQL >  the SELECT  the AVG (. Price) the AS AveragePrice the FROM Products;

2, COUNT (expression) Returns the total number of records query, expression parameter is a field or asterisk

# Return the Products table products field total number of records: 
MySQL >  the SELECT  COUNT (the above mentioned id) AS NumberOfProducts the FROM Products;

3, MAX (expression) returns the largest value of the field expression

# Returns the maximum data field in the Products table in Price:
 MySQL> the SELECT MAX (Price) the AS LargestPrice the FROM Products;

4, MIN (expression) of the expression returns the minimum field

# Returns the data fields in the Products table minimum Price: 
MySQL >  the SELECT  MIN (Price) the AS SmalltPrice the FROM Products;

5, SUM (expression) Returns the specified field sum

Sum # Calculated Cost fields in the Products table: 
MySQL >  the SELECT  the SUM (Cost) the AS TotalCost the FROM Products;

6, group_concat (expression) Returns the field contents splicing

MySQL >  the SELECT GROUP_CONCAT (name) from crm_product;
 + - --------------------- + 
| GROUP_CONCAT (name)     | 
+ - ------ + --------------- 
| iron phosphate lithium battery, the laminated battery | 
+ - --------------------- + 
1 Row in  the SET ( 0.00 sec)

 

Guess you like

Origin www.cnblogs.com/shenjianping/p/11703252.html
Recommended