聚合函数
10 methods对一组行进行操作并返回单个值的函数。
CONCAT(str1, str2, ...)返回 expr 中非 NULL 值的数量。COUNT(*) 计算所有行。
Parameters
| Name | Type | Description |
|---|---|---|
| expr | string | 列或表达式(或 *)。 |
| str2 | string | Second string. |
Returns
string
Example
sql
SELECT CONCAT('Hello', ', ', 'World');
-- 'Hello, World'
SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM users;SUBSTRING(str FROM pos FOR len)返回 expr 在非 NULL 值上的总和。
Parameters
| Name | Type | Description |
|---|---|---|
| expr | string | 数值列或表达式。 |
| pos | int | Start position (1-based). |
| len | int | Length. |
Returns
string
Example
sql
SELECT SUBSTRING('hello world' FROM 1 FOR 5);
-- 'hello'
SELECT SUBSTRING('hello world' FROM 7);
-- 'world'LENGTH(str)返回 expr 在非 NULL 值上的算术平均值。
Parameters
| Name | Type | Description |
|---|---|---|
| expr | string | 数值列或表达式。 |
Returns
int
Example
sql
SELECT LENGTH('hello'); -- 5
SELECT CHAR_LENGTH('café'); -- 4 (characters)
SELECT OCTET_LENGTH('café'); -- 5 (bytes in UTF-8)UPPER(str)返回 expr 在非 NULL 值上的最小值。
Parameters
| Name | Type | Description |
|---|---|---|
| expr | string | 列或表达式。 |
Returns
string
Example
sql
SELECT UPPER('hello'); -- 'HELLO'
SELECT UPPER('café'); -- 'CAFÉ'LOWER(str)返回 expr 在非 NULL 值上的最大值。
Parameters
| Name | Type | Description |
|---|---|---|
| expr | string | 列或表达式。 |
Returns
string
Example
sql
SELECT LOWER('HELLO'); -- 'hello'
SELECT LOWER('CAFÉ'); -- 'café'REPLACE(str, from_str, to_str)将 expr 的非 NULL 值连接为一个字符串。(MySQL;PostgreSQL / SQL Server 中为 STRING_AGG。)
Parameters
| Name | Type | Description |
|---|---|---|
| expr | string | 要连接的列或表达式。 |
| sep | string | 分隔符(默认为 ',')。 |
| to_str | string | Replacement. |
Returns
string
Example
sql
SELECT REPLACE('a-b-c', '-', '_');
-- 'a_b_c'
SELECT REPLACE('hello', 'l', 'L');
-- 'heLLo'TRIM([LEADING|TRAILING|BOTH] [chars] FROM str)Remove leading and/or trailing characters from str. Default removes spaces from both ends.
Parameters
| Name | Type | Description |
|---|---|---|
| chars | string | Characters to remove (default space). |
| str | string | Source string. |
Returns
string
Example
sql
SELECT TRIM(' hi '); -- 'hi'
SELECT TRIM(LEADING '0' FROM '00123'); -- '123'
SELECT TRIM(BOTH 'x' FROM 'xxhixx'); -- 'hi'LEFT(str, n)Return the leftmost n characters of str. (MySQL / SQL Server / PostgreSQL.)
Parameters
| Name | Type | Description |
|---|---|---|
| str | string | Source string. |
| n | int | Number of characters. |
Returns
string
Example
sql
SELECT LEFT('hello', 3); -- 'hel'
SELECT LEFT('abc', 10); -- 'abc'RIGHT(str, n)Return the rightmost n characters of str. (MySQL / SQL Server / PostgreSQL.)
Parameters
| Name | Type | Description |
|---|---|---|
| str | string | Source string. |
| n | int | Number of characters. |
Returns
string
Example
sql
SELECT RIGHT('hello', 3); -- 'llo'
SELECT RIGHT('abc', 10); -- 'abc'CHARINDEX(substr, str)Return the 1-based starting position of substr in str, or 0 if not found. (SQL Server; LOCATE/INSTR in MySQL/PostgreSQL.)
Parameters
| Name | Type | Description |
|---|---|---|
| substr | string | Substring to find. |
| str | string | Source string. |
Returns
int
Example
sql
-- SQL Server
SELECT CHARINDEX('lo', 'hello'); -- 4
SELECT CHARINDEX('z', 'hello'); -- 0
-- MySQL alternative:
SELECT LOCATE('lo', 'hello'); -- 4