Updated on 2024-11-11 GMT+08:00

Functions

The syntax of a complete analysis statement is as follows:

SELECT [DISTINCT] (* | expression) [AS alias] [, ...]
[GROUP BY expression [, ...] [HAVING predicates]]
[ORDER BY expression [ASC | DESC] [, ...]]
[LIMIT size OFFSET offset]

This section describes functions.

Mathematics Functions

Table 1 Mathematics Functions

Function

Purpose

Description

Example Value

abs

Absolute value

abs(number T) -> T

SELECT abs(0.5) LIMIT 1

add

Addition

add(number T, number) -> T

SELECT add(1, 5) LIMIT 1

cbrt

Cubic root

cbrt(number T) -> T

SELECT cbrt(0.5) LIMIT 1

ceil

Rounded up

ceil(number T) -> T

SELECT ceil(0.5) LIMIT 1

divide

Division

divide(number T, number) -> T

SELECT divide(1, 0.5) LIMIT 1

e

Natural base number e

e() -> double

SELECT e() LIMIT 1

exp

Power of the natural base number e

exp(number T) -> T

SELECT exp(0.5) LIMIT 1

expm1

Subtract one from the power of the natural base number e.

expm1(number T) -> T

SELECT expm1(0.5) LIMIT 1

floor

Rounded down

floor(number T) -> T

SELECT floor(0.5) AS Rounded_Down LIMIT 1

ln

Returns the natural logarithm.

ln(number T) -> double

SELECT ln(10) LIMIT 1

log

Logarithm with T as the base

log(number T, number) -> double

SELECT log(10) LIMIT 1

log2

Logarithm with 2 as the base

log2(number T) -> double

SELECT log2(10) LIMIT 1

log10

Logarithm to base 10

log10(number T) -> double

SELECT log10(10) LIMIT 1

mod

Remainder

mod(number T, number) -> T

SELECT modulus(2, 3) LIMIT 1

multiply

Multiplication

multiply(number T, number) -> number

SELECT multiply(2, 3) LIMIT 1

pi

π

pi() -> double

SELECT pi() LIMIT 1

pow

T power of

pow(number T, number) -> T

SELECT pow(2, 3) LIMIT 1

power

T power of

power(number T) -> T, power(number T, number) -> T

SELECT power(2, 3) LIMIT 1

rand

Random number.

rand() -> number, rand(number T) -> T

SELECT rand(5) LIMIT 1

rint

Discard decimals.

rint(number T) -> T

SELECT rint(1.5) LIMIT 1

round

Round off

round(number T) -> T

SELECT round(1.5) LIMIT 1

sign

Symbol

sign(number T) -> T

SELECT sign(1.5) LIMIT 1

signum

Symbol

signum(number T) -> T

SELECT signum(0.5) LIMIT 1

sqrt

Square root

sqrt(number T) -> T

SELECT sqrt(0.5) LIMIT 1

subtract

Subtraction

subtract(number T, number) -> T

SELECT subtract(3, 2) LIMIT 1

/

Division

number / number -> number

SELECT 1 / 100 LIMIT 1

%

Remainder

number % number -> number

SELECT 1 % 100 LIMIT 1

Trigonometric Functions

Table 2 Trigonometric functions

Functions

Purpose

Description

Example Value

acos

Arc cosine

acos(number T) -> double

SELECT acos(0.5) LIMIT 1

asin

Arc sine

asin(number T) -> double

SELECT asin(0.5) LIMIT 1

atan

Inverse tangent

atan(number T) -> double

SELECT atan(0.5) LIMIT 1

atan2

T Arc tangent of the result of dividing U

atan2(number T, number U) -> double

SELECT atan2(1, 0.5) LIMIT 1

cos

Cosine

cos(number T) -> double

SELECT cos(0.5) LIMIT 1

cosh

hyperbolic cosine

cosh(number T) -> double

SELECT cosh(0.5) LIMIT 1

cot

Cotangent

cot(number T) -> double

SELECT cot(0.5) LIMIT 1

degrees

Converting radians to degrees

degrees(number T) -> double

SELECT degrees(0.5) LIMIT 1

radians

Converting degrees to radians

radians(number T) -> double

SELECT radians(0.5) LIMIT 1

sin

Sine

sin(number T) -> double

SELECT sin(0.5) LIMIT 1

sinh

hyperbolic sine

sinh(number T) -> double

SELECT sinh(0.5) LIMIT 1

tan

Tangent

tan(number T) -> double

SELECT tan(0.5) LIMIT 1

Temporal Functions

Table 3 Temporal functions

Function

Purpose

Description

Example Value

curdate

Specifies the current date.

curdate() -> date

SELECT curdate() LIMIT 1

date

Date

date(date) -> date

SELECT date() LIMIT 1

date_format

Obtains the date value based on the format.

date_format(date, string) -> string

SELECT date_format(date, 'Y') LIMIT 1

day_of_month

Month

day_of_month(date) -> integer

SELECT day_of_month(date) LIMIT 1

day_of_week

Day of a week

day_of_week(date) -> integer

SELECT day_of_week(date) LIMIT 1

day_of_year

Number of days in the current year

day_of_year(date) -> integer

SELECT day_of_year(date) LIMIT 1

hour_of_day

Number of hours on the current day

hour_of_day(date) -> integer

SELECT hour_of_day(date) LIMIT 1

maketime

Date of Generation

maketime(integer, integer, integer) -> time

SELECT maketime(11, 30, 00) LIMIT 1

minute_of_hour

Number of minutes in the current hour

minute_of_hour(date) -> integer

SELECT minute_of_hour(date) LIMIT 1

minute_of_day

Number of minutes on the current day

minute_of_day(date) -> integer

SELECT minute_of_day(date) LIMIT 1

monthname

Month Name

monthname(date) -> string

SELECT monthname(date) LIMIT 1

now

Current time.

now() -> time

SELECT now() LIMIT 1

second_of_minute

Number of seconds

minute_of_day(date) -> integer

SELECT minute_of_day(date) LIMIT 1

timestamp

Date

timestamp(date) -> date

SELECT timestamp(date) LIMIT 1

year

Year

year(date) -> integer

SELECT year(date) LIMIT 1

Text Functions

Table 4 Text functions

Function

Purpose

Description

Example Value

ascii

ASCII value of the first character

ascii(string T) -> integer

SELECT ascii('t') LIMIT 1

concat_ws

Connection String

concat_ws(separator, string, string) -> string

SELECT concat_ws('-', 'Tutorial', 'is', 'fun!') LIMIT 1

left

Obtain a character string from left to right.

left(string T, integer) -> T

SELECT left('hello', 2) LIMIT 1

length

length

length(string) -> integer

SELECT length('hello') LIMIT 1

locate

Search for a string

locate(string, string) -> integer

SELECT locate('o', 'hello') LIMIT 1

replace

Replace strings

replace(string T, string, string) -> T

SELECT replace('hello', 'l', 'x') LIMIT 1

right

Obtain a character string from right to left.

right(string T, integer) -> T

SELECT right('hello', 1) LIMIT 1

rtrim

Remove the empty character string on the right.

rtrim(string T) -> T

SELECT rtrim('hello ') LIMIT 1

substring

Obtaining a Substring

substring(string T, integer, integer) -> T

SELECT substring('hello', 2,5) LIMIT 1

trim

Remove empty character strings on both sides.

trim(string T) -> T

SELECT trim(' hello ') LIMIT 1

upper

Convert all letters to uppercase letters.

upper(string T) -> T

SELECT upper('helloworld') LIMIT 1

Other

Table 5 Other

Function

Purpose

Description

Example Value

if

if condition

if(boolean, object, object) -> object

SELECT if(false, 0, 1) LIMIT 1 ,

SELECT if(true, 0, 1) LIMIT 1

ifnull

If the field is null, the default value is used.

ifnull(object, object) -> object

SELECT ifnull('hello', 1) LIMIT 1 ,

SELECT ifnull(null, 1) LIMIT 1

isnull

Indicates whether a field is null. If yes, 1 is returned. If no, 0 is returned.

isnull(object) -> integer

SELECT isnull(null) LIMIT 1 ,

SELECT isnull(1) LIMIT 1