MySQL function

MySQL function

MySQL has many built-in functions and calculations can be used to count
usage:

SELECT function(column_name) FROM table_name;

Aggregate Functions (aggregate functions)

Aggregate operating functions for a range of values, and returns a single value
if many other expressions in the list of items in the SELECT statement in the SELECT statement, you must use the SELECT GROUP BY statement

GROUP BY

GROUP BYStatement is used in conjunction with aggregate functions, grouping result set according to one or more columns
Syntax:

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name

Example:
Table Scores:

+------+---------+-------+
| name | object  | score |
+------+---------+-------+
| Jack | Math    |    90 |
| Jack | English |    80 |
| Tom  | English |   100 |
| Tom  | History |    90 |
| Lucy | Math    |    85 |
| Lucy | English |    85 |
| Lucy | History |    90 |
+------+---------+-------+
SELECT name, AVG(score) FROM score GROUP BY name;

Results of the:

+------+------------+
| name | avg(score) |
+------+------------+
| Jack |    85.0000 |
| Tom  |    95.0000 |
| Lucy |    86.6667 |
+------+------------+

HAVING

Because WHEREyou not and Aggregate Functionsneed to use the combination HAVINGto be screened

SELECT name, AVG(score) FROM score GROUP BY name HAVING AVG(score) > 90;

Results of the:

+------+------------+
| name | avg(score) |
+------+------------+
| tom  |    95.0000 |
+------+------------+

AVG()

Returns the average

SELECT AVG(column_name) FROM table_name;

COUNT()

Returns the number of rows that satisfy the condition

SELECT COUNT(*) FROM table_name;
SELECT COUNT(*) FROM table_name WHERE condition;
SELECT COUNT(DISTINCT column_name) FROM table_name;

MAX() & MIN()

Returns the maximum (minimum) value

SELECT MAX(column_name) FROM table_name;
SELECT MIN(column_name) FROM table_name;

SUM()

Returns the value of the column and

SELECT SUM(column_name) FROM table_name;

Scalar Functions (scalar function)

UCASE & LCASE

The field is converted to uppercase

SELECT UCASE(column_name) FROM table_name;
SELECT LCASE(column_name) FROM table_name;

MID()

Interception field

SELECT MID(column_name, start[, length]) FROM table_name;

start is the start position, from the beginning, a positive number is positive, negative reciprocal of
length is a length taken
out of range without error, but will not intercept character

ROUND()

Specified number of decimal places field

SELECT ROUND(column_name, decimals) FROM table_name;

NOW()

Get the current time

SELECT NOW();

FORMAT()

Formatting field

SELECT FORMAT(123456.789, 2);

Guess you like

Origin www.cnblogs.com/dbf-/p/11408247.html